1 /*****************************************************************************
2 * vout.m: MacOS X video output plugin
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: vout.m,v 1.12 2003/01/15 00:49:49 jlj Exp $
7 * Authors: Colin Delacroix <colin@zoy.org>
8 * Florian G. Pflug <fgp@phlo.org>
9 * Jon Lech Johansen <jon-vl@nanocrew.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 <errno.h> /* ENOMEM */
30 #include <stdlib.h> /* free() */
31 #include <string.h> /* strerror() */
38 #include <Cocoa/Cocoa.h>
39 #include <QuickTime/QuickTime.h>
44 #define QT_MAX_DIRECTBUFFERS 10
51 /* When using I420 output */
52 PlanarPixmapInfoYUV420 pixmap_i420;
55 /*****************************************************************************
57 *****************************************************************************/
58 static int vout_Init ( vout_thread_t * );
59 static void vout_End ( vout_thread_t * );
60 static int vout_Manage ( vout_thread_t * );
61 static void vout_Display ( vout_thread_t *, picture_t * );
63 static int CoSendRequest ( vout_thread_t *, SEL );
64 static int CoCreateWindow ( vout_thread_t * );
65 static int CoDestroyWindow ( vout_thread_t * );
66 static int CoToggleFullscreen ( vout_thread_t * );
68 static void QTScaleMatrix ( vout_thread_t * );
69 static int QTCreateSequence ( vout_thread_t * );
70 static void QTDestroySequence ( vout_thread_t * );
71 static int QTNewPicture ( vout_thread_t *, picture_t * );
72 static void QTFreePicture ( vout_thread_t *, picture_t * );
74 /*****************************************************************************
75 * OpenVideo: allocates MacOS X video thread output method
76 *****************************************************************************
77 * This function allocates and initializes a MacOS X vout method.
78 *****************************************************************************/
79 int E_(OpenVideo) ( vlc_object_t *p_this )
81 vout_thread_t * p_vout = (vout_thread_t *)p_this;
85 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
86 if( p_vout->p_sys == NULL )
88 msg_Err( p_vout, "out of memory" );
92 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
94 /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
95 for( i_timeout = 20 ; i_timeout-- ; )
99 msleep( INTF_IDLE_SLEEP );
105 msg_Err( p_vout, "no MacOS X interface present" );
106 free( p_vout->p_sys );
110 if( [NSApp respondsToSelector: @selector(getIntf)] )
112 intf_thread_t * p_intf;
114 for( i_timeout = 10 ; i_timeout-- ; )
116 if( ( p_intf = [NSApp getIntf] ) == NULL )
118 msleep( INTF_IDLE_SLEEP );
124 msg_Err( p_vout, "MacOS X intf has getIntf, but is NULL" );
125 free( p_vout->p_sys );
130 p_vout->p_sys->h_img_descr =
131 (ImageDescriptionHandle)NewHandleClear( sizeof(ImageDescription) );
132 p_vout->p_sys->p_matrix = (MatrixRecordPtr)malloc( sizeof(MatrixRecord) );
133 p_vout->p_sys->p_fullscreen_state = NULL;
135 p_vout->p_sys->b_mouse_pointer_visible = 1;
137 /* set window size */
138 p_vout->p_sys->s_rect.size.width = p_vout->i_window_width;
139 p_vout->p_sys->s_rect.size.height = p_vout->i_window_height;
141 if( ( err = EnterMovies() ) != noErr )
143 msg_Err( p_vout, "EnterMovies failed: %d", err );
144 free( p_vout->p_sys->p_matrix );
145 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
146 free( p_vout->p_sys );
150 if( vout_ChromaCmp( p_vout->render.i_chroma, VLC_FOURCC('I','4','2','0') ) )
152 err = FindCodec( kYUV420CodecType, bestSpeedCodec,
153 nil, &p_vout->p_sys->img_dc );
154 if( err == noErr && p_vout->p_sys->img_dc != 0 )
156 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
157 p_vout->p_sys->i_codec = kYUV420CodecType;
161 msg_Err( p_vout, "failed to find an appropriate codec" );
166 msg_Err( p_vout, "chroma 0x%08x not supported",
167 p_vout->render.i_chroma );
170 if( p_vout->p_sys->img_dc == 0 )
172 free( p_vout->p_sys->p_matrix );
173 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
174 free( p_vout->p_sys );
178 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
179 NSArray * o_screens = [NSScreen screens];
180 if( [o_screens count] > 0 && var_Type( p_vout, "video-device" ) == 0 )
186 int i_option = config_GetInt( p_vout, "macosx-vdev" );
188 var_Create( p_vout, "video-device", VLC_VAR_STRING |
191 NSEnumerator * o_enumerator = [o_screens objectEnumerator];
193 while( (o_screen = [o_enumerator nextObject]) != NULL )
196 NSRect s_rect = [o_screen frame];
198 snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
199 "%s %d (%dx%d)", _("Screen"), i,
200 (int)s_rect.size.width, (int)s_rect.size.height );
202 val.psz_string = psz_temp;
203 var_Change( p_vout, "video-device", VLC_VAR_ADDCHOICE, &val );
205 if( ( i - 1 ) == i_option )
207 var_Set( p_vout, "video-device", val );
213 var_AddCallback( p_vout, "video-device", vout_VarCallback,
216 val.b_bool = VLC_TRUE;
217 var_Set( p_vout, "intf-change", val );
221 if( CoCreateWindow( p_vout ) )
223 msg_Err( p_vout, "unable to create window" );
224 free( p_vout->p_sys->p_matrix );
225 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
226 free( p_vout->p_sys );
230 p_vout->pf_init = vout_Init;
231 p_vout->pf_end = vout_End;
232 p_vout->pf_manage = vout_Manage;
233 p_vout->pf_render = NULL;
234 p_vout->pf_display = vout_Display;
239 /*****************************************************************************
240 * vout_Init: initialize video thread output method
241 *****************************************************************************/
242 static int vout_Init( vout_thread_t *p_vout )
247 I_OUTPUTPICTURES = 0;
249 /* Initialize the output structure; we already found a codec,
250 * and the corresponding chroma we will be using. Since we can
251 * arbitrary scale, stick to the coordinates and aspect. */
252 p_vout->output.i_width = p_vout->render.i_width;
253 p_vout->output.i_height = p_vout->render.i_height;
254 p_vout->output.i_aspect = p_vout->render.i_aspect;
256 SetPort( p_vout->p_sys->p_qdport );
257 QTScaleMatrix( p_vout );
259 if( QTCreateSequence( p_vout ) )
261 msg_Err( p_vout, "unable to create sequence" );
265 /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
266 while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
270 /* Find an empty picture slot */
271 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
273 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
275 p_pic = p_vout->p_picture + i_index;
280 /* Allocate the picture */
281 if( p_pic == NULL || QTNewPicture( p_vout, p_pic ) )
286 p_pic->i_status = DESTROYED_PICTURE;
287 p_pic->i_type = DIRECT_PICTURE;
289 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
297 /*****************************************************************************
298 * vout_End: terminate video thread output method
299 *****************************************************************************/
300 static void vout_End( vout_thread_t *p_vout )
304 QTDestroySequence( p_vout );
306 /* Free the direct buffers we allocated */
307 for( i_index = I_OUTPUTPICTURES; i_index; )
310 QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
314 /*****************************************************************************
315 * CloseVideo: destroy video thread output method
316 *****************************************************************************/
317 void E_(CloseVideo) ( vlc_object_t *p_this )
319 vout_thread_t * p_vout = (vout_thread_t *)p_this;
321 if( CoDestroyWindow( p_vout ) )
323 msg_Err( p_vout, "unable to destroy window" );
326 if ( p_vout->p_sys->p_fullscreen_state != NULL )
327 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
331 free( p_vout->p_sys->p_matrix );
332 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
334 free( p_vout->p_sys );
337 /*****************************************************************************
338 * vout_Manage: handle events
339 *****************************************************************************
340 * This function should be called regularly by video output thread. It manages
341 * console events. It returns a non null value on error.
342 *****************************************************************************/
343 static int vout_Manage( vout_thread_t *p_vout )
345 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
347 if( CoToggleFullscreen( p_vout ) )
352 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
355 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
357 QTScaleMatrix( p_vout );
358 SetDSequenceMatrix( p_vout->p_sys->i_seq,
359 p_vout->p_sys->p_matrix );
361 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
364 /* hide/show mouse cursor */
365 if( p_vout->p_sys->b_mouse_moved ||
366 p_vout->p_sys->i_time_mouse_last_moved )
368 vlc_bool_t b_change = 0;
370 if( !p_vout->p_sys->b_mouse_pointer_visible )
372 CGDisplayShowCursor( kCGDirectMainDisplay );
376 else if( !p_vout->p_sys->b_mouse_moved &&
377 mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 &&
378 p_vout->p_sys->b_mouse_pointer_visible )
380 CGDisplayHideCursor( kCGDirectMainDisplay );
387 p_vout->p_sys->i_time_mouse_last_moved = 0;
388 p_vout->p_sys->b_mouse_moved = 0;
389 p_vout->p_sys->b_mouse_pointer_visible =
390 !p_vout->p_sys->b_mouse_pointer_visible;
397 /*****************************************************************************
398 * vout_Display: displays previously rendered output
399 *****************************************************************************
400 * This function sends the currently rendered image to the display.
401 *****************************************************************************/
402 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
407 if( ( err = DecompressSequenceFrameS(
408 p_vout->p_sys->i_seq,
409 p_pic->p_sys->p_info,
410 p_pic->p_sys->i_size,
411 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
413 msg_Err( p_vout, "DecompressSequenceFrameS failed: %d", err );
417 QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
421 /*****************************************************************************
422 * CoSendRequest: send request to interface thread
423 *****************************************************************************
424 * Returns 0 on success, 1 otherwise
425 *****************************************************************************/
426 static int CoSendRequest( vout_thread_t *p_vout, SEL sel )
430 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
431 VLCVout * o_vlv = [[[VLCVout alloc] init] autorelease];
433 if( [o_vlv respondsToSelector: @selector(performSelectorOnMainThread:
434 withObject:waitUntilDone:)] )
436 [o_vlv performSelectorOnMainThread: sel
437 withObject: [NSValue valueWithPointer: p_vout]
440 else if( [NSApp respondsToSelector: @selector(getIntf)] )
444 NSPort * o_recv_port;
445 NSInvocation * o_inv;
446 NSPortMessage * o_msg;
447 intf_thread_t * p_intf;
448 NSMethodSignature * o_sig;
450 p_intf = (intf_thread_t *)[NSApp getIntf];
452 o_recv_port = [[NSPort port] retain];
453 o_value = [NSValue valueWithPointer: p_vout];
455 o_sig = [VLCVout instanceMethodSignatureForSelector: sel];
456 o_inv = [NSInvocation invocationWithMethodSignature: o_sig];
457 [o_inv setArgument: &o_value atIndex: 2];
458 [o_inv setTarget: o_vlv];
459 [o_inv setSelector: sel];
461 o_array = [NSArray arrayWithObject:
462 [NSData dataWithBytes: &o_inv length: sizeof(o_inv)]];
463 o_msg = [[NSPortMessage alloc]
464 initWithSendPort: p_intf->p_sys->o_sendport
465 receivePort: o_recv_port components: o_array];
467 p_vout->p_sys->o_lock =
468 [[NSConditionLock alloc] initWithCondition: 0];
469 [o_msg sendBeforeDate: [NSDate distantPast]];
470 [p_vout->p_sys->o_lock lockWhenCondition: 1];
471 [p_vout->p_sys->o_lock unlock];
472 [p_vout->p_sys->o_lock release];
473 p_vout->p_sys->o_lock = nil;
476 [o_recv_port release];
480 msg_Err( p_vout, "SendRequest: no way to communicate with mt" );
489 /*****************************************************************************
490 * CoCreateWindow: create new window
491 *****************************************************************************
492 * Returns 0 on success, 1 otherwise
493 *****************************************************************************/
494 static int CoCreateWindow( vout_thread_t *p_vout )
496 if( CoSendRequest( p_vout, @selector(createWindow:) ) )
498 msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
505 /*****************************************************************************
506 * CoDestroyWindow: destroy window
507 *****************************************************************************
508 * Returns 0 on success, 1 otherwise
509 *****************************************************************************/
510 static int CoDestroyWindow( vout_thread_t *p_vout )
512 if( !p_vout->p_sys->b_mouse_pointer_visible )
514 CGDisplayShowCursor( kCGDirectMainDisplay );
515 p_vout->p_sys->b_mouse_pointer_visible = 1;
518 if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
520 msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
527 /*****************************************************************************
528 * CoToggleFullscreen: toggle fullscreen
529 *****************************************************************************
530 * Returns 0 on success, 1 otherwise
531 *****************************************************************************/
532 static int CoToggleFullscreen( vout_thread_t *p_vout )
534 QTDestroySequence( p_vout );
536 if( CoDestroyWindow( p_vout ) )
538 msg_Err( p_vout, "unable to destroy window" );
542 p_vout->b_fullscreen = !p_vout->b_fullscreen;
544 if( p_vout->b_fullscreen )
546 if ( p_vout->p_sys->p_fullscreen_state == NULL )
547 BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
548 NULL, NULL, fullScreenHideCursor | fullScreenAllowEvents );
552 if ( p_vout->p_sys->p_fullscreen_state != NULL )
553 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
554 p_vout->p_sys->p_fullscreen_state = NULL;
556 config_PutInt( p_vout, "fullscreen", p_vout->b_fullscreen );
558 if( CoCreateWindow( p_vout ) )
560 msg_Err( p_vout, "unable to create window" );
564 SetPort( p_vout->p_sys->p_qdport );
565 QTScaleMatrix( p_vout );
567 if( QTCreateSequence( p_vout ) )
569 msg_Err( p_vout, "unable to create sequence" );
576 /*****************************************************************************
577 * QTScaleMatrix: scale matrix
578 *****************************************************************************/
579 static void QTScaleMatrix( vout_thread_t *p_vout )
582 unsigned int i_width, i_height;
583 Fixed factor_x, factor_y;
584 unsigned int i_offset_x = 0;
585 unsigned int i_offset_y = 0;
587 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
589 i_width = s_rect.right - s_rect.left;
590 i_height = s_rect.bottom - s_rect.top;
592 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
594 int i_adj_width = i_height * p_vout->output.i_aspect /
597 factor_x = FixDiv( Long2Fix( i_adj_width ),
598 Long2Fix( p_vout->output.i_width ) );
599 factor_y = FixDiv( Long2Fix( i_height ),
600 Long2Fix( p_vout->output.i_height ) );
602 i_offset_x = (i_width - i_adj_width) / 2;
606 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
607 p_vout->output.i_aspect;
609 factor_x = FixDiv( Long2Fix( i_width ),
610 Long2Fix( p_vout->output.i_width ) );
611 factor_y = FixDiv( Long2Fix( i_adj_height ),
612 Long2Fix( p_vout->output.i_height ) );
614 i_offset_y = (i_height - i_adj_height) / 2;
617 SetIdentityMatrix( p_vout->p_sys->p_matrix );
619 ScaleMatrix( p_vout->p_sys->p_matrix,
621 Long2Fix(0), Long2Fix(0) );
623 TranslateMatrix( p_vout->p_sys->p_matrix,
624 Long2Fix(i_offset_x),
625 Long2Fix(i_offset_y) );
628 /*****************************************************************************
629 * QTCreateSequence: create a new sequence
630 *****************************************************************************
631 * Returns 0 on success, 1 otherwise
632 *****************************************************************************/
633 static int QTCreateSequence( vout_thread_t *p_vout )
636 ImageDescriptionPtr p_descr;
638 HLock( (Handle)p_vout->p_sys->h_img_descr );
639 p_descr = *p_vout->p_sys->h_img_descr;
641 p_descr->idSize = sizeof(ImageDescription);
642 p_descr->cType = p_vout->p_sys->i_codec;
643 p_descr->version = 1;
644 p_descr->revisionLevel = 0;
645 p_descr->vendor = 'appl';
646 p_descr->width = p_vout->output.i_width;
647 p_descr->height = p_vout->output.i_height;
648 p_descr->hRes = Long2Fix(72);
649 p_descr->vRes = Long2Fix(72);
650 p_descr->spatialQuality = codecLosslessQuality;
651 p_descr->frameCount = 1;
652 p_descr->clutID = -1;
653 p_descr->dataSize = 0;
656 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
658 if( ( err = DecompressSequenceBeginS(
659 &p_vout->p_sys->i_seq,
660 p_vout->p_sys->h_img_descr,
662 p_vout->p_sys->p_qdport,
664 p_vout->p_sys->p_matrix,
666 codecFlagUseImageBuffer,
667 codecLosslessQuality,
668 p_vout->p_sys->img_dc ) ) )
670 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
677 /*****************************************************************************
678 * QTDestroySequence: destroy sequence
679 *****************************************************************************/
680 static void QTDestroySequence( vout_thread_t *p_vout )
682 CDSequenceEnd( p_vout->p_sys->i_seq );
685 /*****************************************************************************
686 * QTNewPicture: allocate a picture
687 *****************************************************************************
688 * Returns 0 on success, 1 otherwise
689 *****************************************************************************/
690 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
692 int i_width = p_vout->output.i_width;
693 int i_height = p_vout->output.i_height;
695 /* We know the chroma, allocate a buffer which will be used
696 * directly by the decoder */
697 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
699 if( p_pic->p_sys == NULL )
704 switch( p_vout->output.i_chroma )
706 case VLC_FOURCC('I','4','2','0'):
708 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
709 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
711 /* Allocate the memory buffer */
712 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
713 16, i_width * i_height * 3 / 2 );
716 p_pic->Y_PIXELS = p_pic->p_data;
717 p_pic->p[Y_PLANE].i_lines = i_height;
718 p_pic->p[Y_PLANE].i_pitch = i_width;
719 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
720 p_pic->p[Y_PLANE].i_visible_pitch = i_width;
723 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
724 p_pic->p[U_PLANE].i_lines = i_height / 2;
725 p_pic->p[U_PLANE].i_pitch = i_width / 2;
726 p_pic->p[U_PLANE].i_pixel_pitch = 1;
727 p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
730 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
731 p_pic->p[V_PLANE].i_lines = i_height / 2;
732 p_pic->p[V_PLANE].i_pitch = i_width / 2;
733 p_pic->p[V_PLANE].i_pixel_pitch = 1;
734 p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
736 /* We allocated 3 planes */
739 #define P p_pic->p_sys->pixmap_i420
740 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
741 - p_pic->p_sys->p_info;
742 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
743 - p_pic->p_sys->p_info;
744 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
745 - p_pic->p_sys->p_info;
747 P.componentInfoY.rowBytes = i_width;
748 P.componentInfoCb.rowBytes = i_width / 2;
749 P.componentInfoCr.rowBytes = i_width / 2;
755 /* Unknown chroma, tell the guy to get lost */
756 free( p_pic->p_sys );
757 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
758 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
766 /*****************************************************************************
767 * QTFreePicture: destroy a picture allocated with QTNewPicture
768 *****************************************************************************/
769 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
771 switch( p_vout->output.i_chroma )
773 case VLC_FOURCC('I','4','2','0'):
774 free( p_pic->p_data_orig );
778 free( p_pic->p_sys );
781 /*****************************************************************************
782 * VLCWindow implementation
783 *****************************************************************************/
784 @implementation VLCWindow
786 - (void)setVout:(vout_thread_t *)_p_vout
791 - (vout_thread_t *)getVout
796 - (void)toggleFullscreen
798 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
803 return( p_vout->b_fullscreen );
806 - (BOOL)canBecomeKeyWindow
811 - (void)keyDown:(NSEvent *)o_event
815 if( [[o_event characters] length] )
817 key = [[o_event characters] characterAtIndex: 0];
822 case (unichar)0xf700: /* up-arrow */
824 aout_instance_t * p_aout = vlc_object_find( p_vout, VLC_OBJECT_AOUT,
826 if ( p_aout != NULL )
828 aout_VolumeUp( p_aout, 1, NULL );
829 vlc_object_release( (vlc_object_t *)p_aout );
834 case (unichar)0xf701: /* down-arrow */
836 aout_instance_t * p_aout = vlc_object_find( p_vout, VLC_OBJECT_AOUT,
838 if ( p_aout != NULL )
840 aout_VolumeDown( p_aout, 1, NULL );
841 vlc_object_release( (vlc_object_t *)p_aout );
847 [self toggleFullscreen];
850 case (unichar)0x1b: /* escape */
851 if( [self isFullscreen] )
853 [self toggleFullscreen];
858 p_vout->p_vlc->b_die = VLC_TRUE;
862 input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
866 [super keyDown: o_event];
871 /* This is actually the same as VLCControls::stop. */
872 - (BOOL)windowShouldClose:(id)sender
874 intf_thread_t * p_intf = [NSApp getIntf];
875 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
877 if( p_playlist == NULL )
882 playlist_Stop( p_playlist );
883 vlc_object_release( p_playlist );
884 p_intf->p_sys->b_stopping = 1;
886 /* The window will be closed by the intf later. */
892 /*****************************************************************************
893 * VLCView implementation
894 *****************************************************************************/
895 @implementation VLCView
897 - (void)drawRect:(NSRect)rect
899 vout_thread_t * p_vout;
900 id o_window = [self window];
901 p_vout = (vout_thread_t *)[o_window getVout];
903 [[NSColor blackColor] set];
905 [super drawRect: rect];
907 p_vout->i_changes |= VOUT_SIZE_CHANGE;
910 - (BOOL)acceptsFirstResponder
915 - (BOOL)becomeFirstResponder
917 [[self window] setAcceptsMouseMovedEvents: YES];
921 - (BOOL)resignFirstResponder
923 [[self window] setAcceptsMouseMovedEvents: NO];
927 - (void)mouseUp:(NSEvent *)o_event
929 vout_thread_t * p_vout;
930 id o_window = [self window];
931 p_vout = (vout_thread_t *)[o_window getVout];
933 switch( [o_event type] )
938 val.b_bool = VLC_TRUE;
939 var_Set( p_vout, "mouse-clicked", val );
944 [super mouseUp: o_event];
949 - (void)mouseMoved:(NSEvent *)o_event
955 vout_thread_t * p_vout;
956 id o_window = [self window];
957 p_vout = (vout_thread_t *)[o_window getVout];
959 s_rect = [self bounds];
960 ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
961 b_inside = [self mouse: ml inRect: s_rect];
966 int i_width, i_height, i_x, i_y;
968 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
969 (unsigned int)s_rect.size.height,
970 &i_x, &i_y, &i_width, &i_height );
972 val.i_int = ( ((int)ml.x) - i_x ) *
973 p_vout->render.i_width / i_width;
974 var_Set( p_vout, "mouse-x", val );
976 val.i_int = ( ((int)ml.y) - i_y ) *
977 p_vout->render.i_height / i_height;
978 var_Set( p_vout, "mouse-y", val );
980 val.b_bool = VLC_TRUE;
981 var_Set( p_vout, "mouse-moved", val );
985 [super mouseMoved: o_event];
991 /*****************************************************************************
992 * VLCVout implementation
993 *****************************************************************************/
994 @implementation VLCVout
996 - (void)createWindow:(NSValue *)o_value
1000 NSScreen * o_screen;
1001 vout_thread_t * p_vout;
1003 p_vout = (vout_thread_t *)[o_value pointerValue];
1005 p_vout->p_sys->o_window = [VLCWindow alloc];
1006 [p_vout->p_sys->o_window setVout: p_vout];
1007 [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1009 if( var_Get( p_vout, "video-device", &val ) < 0 )
1011 o_screen = [NSScreen mainScreen];
1015 unsigned int i_index = 0;
1016 NSArray *o_screens = [NSScreen screens];
1018 if( !sscanf( val.psz_string, "Screen %d", &i_index ) ||
1019 [o_screens count] < i_index )
1021 o_screen = [NSScreen mainScreen];
1026 o_screen = [o_screens objectAtIndex: i_index];
1027 config_PutInt( p_vout, "macosx-vdev", i_index );
1030 free( val.psz_string );
1033 if( p_vout->b_fullscreen )
1035 NSRect screen_rect = [o_screen frame];
1036 screen_rect.origin.x = screen_rect.origin.y = 0;
1038 [p_vout->p_sys->o_window
1039 initWithContentRect: screen_rect
1040 styleMask: NSBorderlessWindowMask
1041 backing: NSBackingStoreBuffered
1042 defer: NO screen: o_screen];
1044 [p_vout->p_sys->o_window setLevel: NSModalPanelWindowLevel];
1048 unsigned int i_stylemask = NSTitledWindowMask |
1049 NSMiniaturizableWindowMask |
1050 NSClosableWindowMask |
1051 NSResizableWindowMask;
1053 [p_vout->p_sys->o_window
1054 initWithContentRect: p_vout->p_sys->s_rect
1055 styleMask: i_stylemask
1056 backing: NSBackingStoreBuffered
1057 defer: NO screen: o_screen];
1059 if( !p_vout->p_sys->b_pos_saved )
1061 [p_vout->p_sys->o_window center];
1065 o_view = [[VLCView alloc] init];
1066 /* FIXME: [o_view setMenu:] */
1067 [p_vout->p_sys->o_window setContentView: o_view];
1068 [o_view autorelease];
1071 p_vout->p_sys->p_qdport = [o_view qdPort];
1072 [o_view unlockFocus];
1074 [p_vout->p_sys->o_window setTitle:
1075 [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
1076 [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1079 - (void)destroyWindow:(NSValue *)o_value
1081 vout_thread_t * p_vout;
1083 p_vout = (vout_thread_t *)[o_value pointerValue];
1085 if( !p_vout->b_fullscreen )
1089 s_rect = [[p_vout->p_sys->o_window contentView] frame];
1090 p_vout->p_sys->s_rect.size = s_rect.size;
1092 s_rect = [p_vout->p_sys->o_window frame];
1093 p_vout->p_sys->s_rect.origin = s_rect.origin;
1095 p_vout->p_sys->b_pos_saved = 1;
1098 p_vout->p_sys->p_qdport = nil;
1099 [p_vout->p_sys->o_window close];
1100 p_vout->p_sys->o_window = nil;