2 /* vout.m: MacOS X video output plugin
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: vout.m,v 1.53 2003/08/20 13:08:10 garf Exp $
7 * Authors: Colin Delacroix <colin@zoy.org>
8 * Florian G. Pflug <fgp@phlo.org>
9 * Jon Lech Johansen <jon-vl@nanocrew.net>
10 * Derk-Jan Hartman <thedj@users.sourceforge.net>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <errno.h> /* ENOMEM */
31 #include <stdlib.h> /* free() */
32 #include <string.h> /* strerror() */
34 #include <QuickTime/QuickTime.h>
39 #define QT_MAX_DIRECTBUFFERS 10
40 #define VL_MAX_DISPLAYS 16
47 /* When using I420 output */
48 PlanarPixmapInfoYUV420 pixmap_i420;
51 /*****************************************************************************
53 *****************************************************************************/
55 static int vout_Init ( vout_thread_t * );
56 static void vout_End ( vout_thread_t * );
57 static int vout_Manage ( vout_thread_t * );
58 static void vout_Display ( vout_thread_t *, picture_t * );
60 static int CoSendRequest ( vout_thread_t *, SEL );
61 static int CoCreateWindow ( vout_thread_t * );
62 static int CoDestroyWindow ( vout_thread_t * );
63 static int CoToggleFullscreen ( vout_thread_t * );
65 static void VLCHideMouse ( vout_thread_t *, BOOL );
67 static void QTScaleMatrix ( vout_thread_t * );
68 static int QTCreateSequence ( vout_thread_t * );
69 static void QTDestroySequence ( vout_thread_t * );
70 static int QTNewPicture ( vout_thread_t *, picture_t * );
71 static void QTFreePicture ( vout_thread_t *, picture_t * );
73 /*****************************************************************************
74 * OpenVideo: allocates MacOS X video thread output method
75 *****************************************************************************
76 * This function allocates and initializes a MacOS X vout method.
77 *****************************************************************************/
78 int E_(OpenVideo) ( vlc_object_t *p_this )
80 vout_thread_t * p_vout = (vout_thread_t *)p_this;
83 vlc_value_t value_drawable;
85 var_Get( p_vout->p_vlc, "drawable", &value_drawable );
87 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
88 if( p_vout->p_sys == NULL )
90 msg_Err( p_vout, "out of memory" );
94 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
96 /* We don't need an intf in mozilla plugin */
97 if( value_drawable.i_int == 0 )
99 /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
100 for( i_timeout = 20 ; i_timeout-- ; )
104 msleep( INTF_IDLE_SLEEP );
110 /* no MacOS X intf, unable to communicate with MT */
111 msg_Err( p_vout, "no MacOS X interface present" );
112 free( p_vout->p_sys );
117 if( [NSApp respondsToSelector: @selector(getIntf)] )
119 intf_thread_t * p_intf;
121 for( i_timeout = 10 ; i_timeout-- ; )
123 if( ( p_intf = [NSApp getIntf] ) == NULL )
125 msleep( INTF_IDLE_SLEEP );
131 msg_Err( p_vout, "MacOS X intf has getIntf, but is NULL" );
132 free( p_vout->p_sys );
138 p_vout->p_sys->h_img_descr =
139 (ImageDescriptionHandle)NewHandleClear( sizeof(ImageDescription) );
140 p_vout->p_sys->p_matrix = (MatrixRecordPtr)malloc( sizeof(MatrixRecord) );
141 p_vout->p_sys->p_fullscreen_state = NULL;
143 p_vout->p_sys->b_mouse_pointer_visible = YES;
144 p_vout->p_sys->b_mouse_moved = YES;
145 p_vout->p_sys->i_time_mouse_last_moved = mdate();
147 if( value_drawable.i_int != 0 )
149 p_vout->p_sys->mask = NewRgn();
150 p_vout->p_sys->rect.left = 0 ;
151 p_vout->p_sys->rect.right = 0 ;
152 p_vout->p_sys->rect.top = 0 ;
153 p_vout->p_sys->rect.bottom = 0 ;
155 p_vout->p_sys->isplugin = 1 ;
159 p_vout->p_sys->mask = NULL;
160 p_vout->p_sys->isplugin = 0 ;
163 /* set window size */
164 p_vout->p_sys->s_rect.size.width = p_vout->i_window_width;
165 p_vout->p_sys->s_rect.size.height = p_vout->i_window_height;
167 if( ( err = EnterMovies() ) != noErr )
169 msg_Err( p_vout, "EnterMovies failed: %d", err );
170 free( p_vout->p_sys->p_matrix );
171 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
172 free( p_vout->p_sys );
176 /* Damn QT isn't thread safe. so keep a lock in the p_vlc object */
177 vlc_mutex_lock( &p_vout->p_vlc->quicktime_lock );
179 err = FindCodec( kYUV420CodecType, bestSpeedCodec,
180 nil, &p_vout->p_sys->img_dc );
182 vlc_mutex_unlock( &p_vout->p_vlc->quicktime_lock );
183 if( err == noErr && p_vout->p_sys->img_dc != 0 )
185 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
186 p_vout->p_sys->i_codec = kYUV420CodecType;
190 msg_Err( p_vout, "failed to find an appropriate codec" );
193 if( p_vout->p_sys->img_dc == 0 )
195 free( p_vout->p_sys->p_matrix );
196 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
197 free( p_vout->p_sys );
201 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
202 NSArray * o_screens = [NSScreen screens];
203 if( [o_screens count] > 0 && var_Type( p_vout, "video-device" ) == 0 )
206 vlc_value_t val, text;
209 int i_option = config_GetInt( p_vout, "macosx-vdev" );
211 var_Create( p_vout, "video-device", VLC_VAR_INTEGER |
213 text.psz_string = _("Video device");
214 var_Change( p_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL );
216 NSEnumerator * o_enumerator = [o_screens objectEnumerator];
218 while( (o_screen = [o_enumerator nextObject]) != NULL )
221 NSRect s_rect = [o_screen frame];
223 snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
224 "%s %d (%dx%d)", _("Screen"), i,
225 (int)s_rect.size.width, (int)s_rect.size.height );
227 text.psz_string = psz_temp;
229 var_Change( p_vout, "video-device",
230 VLC_VAR_ADDCHOICE, &val, &text );
232 if( ( i - 1 ) == i_option )
234 var_Set( p_vout, "video-device", val );
239 var_AddCallback( p_vout, "video-device", vout_VarCallback,
242 val.b_bool = VLC_TRUE;
243 var_Set( p_vout, "intf-change", val );
247 /* We don't need a window either in the mozilla plugin */
248 if( p_vout->p_sys->isplugin == 0 )
250 if( CoCreateWindow( p_vout ) )
252 msg_Err( p_vout, "unable to create window" );
253 free( p_vout->p_sys->p_matrix );
254 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
255 free( p_vout->p_sys );
260 p_vout->pf_init = vout_Init;
261 p_vout->pf_end = vout_End;
262 p_vout->pf_manage = vout_Manage;
263 p_vout->pf_render = NULL;
264 p_vout->pf_display = vout_Display;
269 /*****************************************************************************
270 * vout_Init: initialize video thread output method
271 *****************************************************************************/
272 static int vout_Init( vout_thread_t *p_vout )
278 I_OUTPUTPICTURES = 0;
280 /* Initialize the output structure; we already found a codec,
281 * and the corresponding chroma we will be using. Since we can
282 * arbitrary scale, stick to the coordinates and aspect. */
283 p_vout->output.i_width = p_vout->render.i_width;
284 p_vout->output.i_height = p_vout->render.i_height;
285 p_vout->output.i_aspect = p_vout->render.i_aspect;
287 var_Get( p_vout->p_vlc, "drawable", &val );
289 if( p_vout->p_sys->isplugin )
291 p_vout->p_sys->p_qdport = val.i_int;
294 SetPort( p_vout->p_sys->p_qdport );
295 QTScaleMatrix( p_vout );
297 if( QTCreateSequence( p_vout ) )
299 msg_Err( p_vout, "unable to create sequence" );
303 /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
304 while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
308 /* Find an empty picture slot */
309 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
311 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
313 p_pic = p_vout->p_picture + i_index;
318 /* Allocate the picture */
319 if( p_pic == NULL || QTNewPicture( p_vout, p_pic ) )
324 p_pic->i_status = DESTROYED_PICTURE;
325 p_pic->i_type = DIRECT_PICTURE;
327 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
335 /*****************************************************************************
336 * vout_End: terminate video thread output method
337 *****************************************************************************/
338 static void vout_End( vout_thread_t *p_vout )
342 QTDestroySequence( p_vout );
344 /* Free the direct buffers we allocated */
345 for( i_index = I_OUTPUTPICTURES; i_index; )
348 QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
352 /*****************************************************************************
353 * CloseVideo: destroy video thread output method
354 *****************************************************************************/
355 void E_(CloseVideo) ( vlc_object_t *p_this )
357 vout_thread_t * p_vout = (vout_thread_t *)p_this;
359 if ( p_vout->p_sys->isplugin == 0)
361 if( CoDestroyWindow( p_vout ) )
363 msg_Err( p_vout, "unable to destroy window" );
367 if ( p_vout->p_sys->p_fullscreen_state != NULL )
368 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
372 free( p_vout->p_sys->p_matrix );
373 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
375 free( p_vout->p_sys );
378 /*****************************************************************************
379 * vout_Manage: handle events
380 *****************************************************************************
381 * This function should be called regularly by video output thread. It manages
382 * console events. It returns a non null value on error.
383 *****************************************************************************/
384 static int vout_Manage( vout_thread_t *p_vout )
387 var_Get( p_vout->p_vlc, "drawableredraw", &val1 );
389 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
391 if( CoToggleFullscreen( p_vout ) )
396 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
399 if( (p_vout->i_changes & VOUT_SIZE_CHANGE) || (val1.i_int == 1))
404 var_Set( p_vout->p_vlc, "drawableredraw", val1 );
405 SetDSequenceMask( p_vout->p_sys->i_seq , p_vout->p_sys->mask );
406 } else if (p_vout->i_changes & VOUT_SIZE_CHANGE)
408 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
411 QTScaleMatrix( p_vout );
412 SetDSequenceMatrix( p_vout->p_sys->i_seq,
413 p_vout->p_sys->p_matrix );
416 /* hide/show mouse cursor
417 * this code looks unnecessarily complicated, but is necessary like this.
418 * it has to deal with multiple monitors and therefore checks a lot */
419 if( !p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
421 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 &&
422 p_vout->p_sys->b_mouse_pointer_visible )
424 VLCHideMouse( p_vout, YES );
426 else if ( !p_vout->p_sys->b_mouse_pointer_visible )
428 vlc_bool_t b_playing = NO;
429 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
432 if ( p_playlist != nil )
434 vlc_mutex_lock( &p_playlist->object_lock );
435 if( p_playlist->p_input != NULL )
437 vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
438 b_playing = p_playlist->p_input->stream.control.i_status != PAUSE_S;
439 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
441 vlc_mutex_unlock( &p_playlist->object_lock );
442 vlc_object_release( p_playlist );
446 VLCHideMouse( p_vout, NO );
450 else if ( p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
452 if( !p_vout->p_sys->b_mouse_pointer_visible )
454 VLCHideMouse( p_vout, NO );
458 p_vout->p_sys->b_mouse_moved = NO;
465 /*****************************************************************************
466 * vout_Display: displays previously rendered output
467 *****************************************************************************
468 * This function sends the currently rendered image to the display.
469 *****************************************************************************/
470 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
477 if( p_vout->p_sys->isplugin )
481 /* In mozilla plugin, mozilla browser also draws things in
482 * the windows. So we have to update the port/Origin for each
483 * picture. FIXME : the vout should lock something ! */
484 GetPort( &p_vout->p_sys->p_qdportold );
485 GetPortBounds( p_vout->p_sys->p_qdportold, &oldrect );
488 SetPort( p_vout->p_sys->p_qdport );
489 SetOrigin( p_vout->p_sys->portx , p_vout->p_sys->porty );
490 ClipRect( &p_vout->p_sys->rect );
492 if( ( err = DecompressSequenceFrameS(
493 p_vout->p_sys->i_seq,
494 p_pic->p_sys->p_info,
495 p_pic->p_sys->i_size,
496 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
498 msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
502 QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
505 SetOrigin( oldrect.left , oldrect.top );
507 SetPort( p_vout->p_sys->p_qdportold );
511 if( ( err = DecompressSequenceFrameS(
512 p_vout->p_sys->i_seq,
513 p_pic->p_sys->p_info,
514 p_pic->p_sys->i_size,
515 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
517 msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
521 QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
526 /*****************************************************************************
527 * CoSendRequest: send request to interface thread
528 *****************************************************************************
529 * Returns 0 on success, 1 otherwise
530 *****************************************************************************/
531 static int CoSendRequest( vout_thread_t *p_vout, SEL sel )
535 VLCVout * o_vlv = [[VLCVout alloc] init];
537 if( ( i_ret = ExecuteOnMainThread( o_vlv, sel, (void *)p_vout ) ) )
539 msg_Err( p_vout, "SendRequest: no way to communicate with mt" );
547 /*****************************************************************************
548 * CoCreateWindow: create new window
549 *****************************************************************************
550 * Returns 0 on success, 1 otherwise
551 *****************************************************************************/
552 static int CoCreateWindow( vout_thread_t *p_vout )
554 if( CoSendRequest( p_vout, @selector(createWindow:) ) )
556 msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
563 /*****************************************************************************
564 * CoDestroyWindow: destroy window
565 *****************************************************************************
566 * Returns 0 on success, 1 otherwise
567 *****************************************************************************/
568 static int CoDestroyWindow( vout_thread_t *p_vout )
570 if( !p_vout->p_sys->b_mouse_pointer_visible )
572 VLCHideMouse( p_vout, NO );
575 if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
577 msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
584 /*****************************************************************************
585 * CoToggleFullscreen: toggle fullscreen
586 *****************************************************************************
587 * Returns 0 on success, 1 otherwise
588 *****************************************************************************/
589 static int CoToggleFullscreen( vout_thread_t *p_vout )
591 QTDestroySequence( p_vout );
593 if( CoDestroyWindow( p_vout ) )
595 msg_Err( p_vout, "unable to destroy window" );
599 p_vout->b_fullscreen = !p_vout->b_fullscreen;
601 config_PutInt( p_vout, "fullscreen", p_vout->b_fullscreen );
603 if( CoCreateWindow( p_vout ) )
605 msg_Err( p_vout, "unable to create window" );
609 if( QTCreateSequence( p_vout ) )
611 msg_Err( p_vout, "unable to create sequence" );
618 /*****************************************************************************
619 * VLCHideMouse: if b_hide then hide the cursor
620 *****************************************************************************/
621 static void VLCHideMouse ( vout_thread_t *p_vout, BOOL b_hide )
626 NSWindow *o_window = p_vout->p_sys->o_window;
627 NSView *o_contents = [o_window contentView];
629 s_rect = [o_contents bounds];
630 ml = [o_window convertScreenToBase:[NSEvent mouseLocation]];
631 ml = [o_contents convertPoint:ml fromView:nil];
632 b_inside = [o_contents mouse: ml inRect: s_rect];
634 if ( b_hide && b_inside )
636 /* only hide if mouse over VLCView */
638 p_vout->p_sys->b_mouse_pointer_visible = 0;
643 p_vout->p_sys->b_mouse_pointer_visible = 1;
645 p_vout->p_sys->b_mouse_moved = NO;
646 p_vout->p_sys->i_time_mouse_last_moved = mdate();
650 /*****************************************************************************
651 * QTScaleMatrix: scale matrix
652 *****************************************************************************/
653 static void QTScaleMatrix( vout_thread_t *p_vout )
656 unsigned int i_width, i_height;
657 Fixed factor_x, factor_y;
658 unsigned int i_offset_x = 0;
659 unsigned int i_offset_y = 0;
669 vlc_value_t valportx;
670 vlc_value_t valporty;
672 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
673 i_width = s_rect.right - s_rect.left;
674 i_height = s_rect.bottom - s_rect.top;
676 var_Get( p_vout->p_vlc, "drawable", &val );
677 var_Get( p_vout->p_vlc, "drawablet", &valt );
678 var_Get( p_vout->p_vlc, "drawablel", &vall );
679 var_Get( p_vout->p_vlc, "drawableb", &valb );
680 var_Get( p_vout->p_vlc, "drawabler", &valr );
681 var_Get( p_vout->p_vlc, "drawablex", &valx );
682 var_Get( p_vout->p_vlc, "drawabley", &valy );
683 var_Get( p_vout->p_vlc, "drawablew", &valw );
684 var_Get( p_vout->p_vlc, "drawableh", &valh );
685 var_Get( p_vout->p_vlc, "drawableportx", &valportx );
686 var_Get( p_vout->p_vlc, "drawableporty", &valporty );
688 if( p_vout->p_sys->isplugin )
690 p_vout->p_sys->portx = valportx.i_int;
691 p_vout->p_sys->porty = valporty.i_int;
692 p_vout->p_sys->p_qdport = val.i_int;
693 i_width = valw.i_int;
694 i_height = valh.i_int;
696 SetRectRgn( p_vout->p_sys->mask , vall.i_int - valx.i_int , valt.i_int - valy.i_int , valr.i_int - valx.i_int , valb.i_int - valy.i_int );
697 p_vout->p_sys->rect.top = 0;
698 p_vout->p_sys->rect.left = 0;
699 p_vout->p_sys->rect.bottom = valb.i_int - valt.i_int;
700 p_vout->p_sys->rect.right = valr.i_int - vall.i_int;
703 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
705 int i_adj_width = i_height * p_vout->output.i_aspect /
708 factor_x = FixDiv( Long2Fix( i_adj_width ),
709 Long2Fix( p_vout->output.i_width ) );
710 factor_y = FixDiv( Long2Fix( i_height ),
711 Long2Fix( p_vout->output.i_height ) );
713 i_offset_x = (i_width - i_adj_width) / 2 + i_offset_x;
717 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
718 p_vout->output.i_aspect;
720 factor_x = FixDiv( Long2Fix( i_width ),
721 Long2Fix( p_vout->output.i_width ) );
722 factor_y = FixDiv( Long2Fix( i_adj_height ),
723 Long2Fix( p_vout->output.i_height ) );
725 i_offset_y = (i_height - i_adj_height) / 2 + i_offset_y;
728 SetIdentityMatrix( p_vout->p_sys->p_matrix );
730 ScaleMatrix( p_vout->p_sys->p_matrix,
732 Long2Fix(0), Long2Fix(0) );
734 TranslateMatrix( p_vout->p_sys->p_matrix,
735 Long2Fix(i_offset_x),
736 Long2Fix(i_offset_y) );
740 /*****************************************************************************
741 * QTCreateSequence: create a new sequence
742 *****************************************************************************
743 * Returns 0 on success, 1 otherwise
744 *****************************************************************************/
745 static int QTCreateSequence( vout_thread_t *p_vout )
748 ImageDescriptionPtr p_descr;
750 HLock( (Handle)p_vout->p_sys->h_img_descr );
751 p_descr = *p_vout->p_sys->h_img_descr;
753 p_descr->idSize = sizeof(ImageDescription);
754 p_descr->cType = p_vout->p_sys->i_codec;
755 p_descr->version = 1;
756 p_descr->revisionLevel = 0;
757 p_descr->vendor = 'appl';
758 p_descr->width = p_vout->output.i_width;
759 p_descr->height = p_vout->output.i_height;
760 p_descr->hRes = Long2Fix(72);
761 p_descr->vRes = Long2Fix(72);
762 p_descr->spatialQuality = codecLosslessQuality;
763 p_descr->frameCount = 1;
764 p_descr->clutID = -1;
765 p_descr->dataSize = 0;
769 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
772 if( ( err = DecompressSequenceBeginS(
773 &p_vout->p_sys->i_seq,
774 p_vout->p_sys->h_img_descr,
776 p_vout->p_sys->p_qdport,
778 p_vout->p_sys->p_matrix,
779 0, p_vout->p_sys->mask,
780 codecFlagUseImageBuffer,
781 codecLosslessQuality,
782 p_vout->p_sys->img_dc ) ) )
784 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
792 /*****************************************************************************
793 * QTDestroySequence: destroy sequence
794 *****************************************************************************/
795 static void QTDestroySequence( vout_thread_t *p_vout )
797 CDSequenceEnd( p_vout->p_sys->i_seq );
800 /*****************************************************************************
801 * QTNewPicture: allocate a picture
802 *****************************************************************************
803 * Returns 0 on success, 1 otherwise
804 *****************************************************************************/
805 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
807 int i_width = p_vout->output.i_width;
808 int i_height = p_vout->output.i_height;
810 /* We know the chroma, allocate a buffer which will be used
811 * directly by the decoder */
812 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
814 if( p_pic->p_sys == NULL )
819 switch( p_vout->output.i_chroma )
821 case VLC_FOURCC('I','4','2','0'):
823 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
824 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
826 /* Allocate the memory buffer */
827 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
828 16, i_width * i_height * 3 / 2 );
831 p_pic->Y_PIXELS = p_pic->p_data;
832 p_pic->p[Y_PLANE].i_lines = i_height;
833 p_pic->p[Y_PLANE].i_pitch = i_width;
834 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
835 p_pic->p[Y_PLANE].i_visible_pitch = i_width;
838 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
839 p_pic->p[U_PLANE].i_lines = i_height / 2;
840 p_pic->p[U_PLANE].i_pitch = i_width / 2;
841 p_pic->p[U_PLANE].i_pixel_pitch = 1;
842 p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
845 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
846 p_pic->p[V_PLANE].i_lines = i_height / 2;
847 p_pic->p[V_PLANE].i_pitch = i_width / 2;
848 p_pic->p[V_PLANE].i_pixel_pitch = 1;
849 p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
851 /* We allocated 3 planes */
854 #define P p_pic->p_sys->pixmap_i420
855 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
856 - p_pic->p_sys->p_info;
857 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
858 - p_pic->p_sys->p_info;
859 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
860 - p_pic->p_sys->p_info;
862 P.componentInfoY.rowBytes = i_width;
863 P.componentInfoCb.rowBytes = i_width / 2;
864 P.componentInfoCr.rowBytes = i_width / 2;
870 /* Unknown chroma, tell the guy to get lost */
871 free( p_pic->p_sys );
872 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
873 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
881 /*****************************************************************************
882 * QTFreePicture: destroy a picture allocated with QTNewPicture
883 *****************************************************************************/
884 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
886 switch( p_vout->output.i_chroma )
888 case VLC_FOURCC('I','4','2','0'):
889 free( p_pic->p_data_orig );
893 free( p_pic->p_sys );
896 /*****************************************************************************
897 * VLCWindow implementation
898 *****************************************************************************/
899 @implementation VLCWindow
901 - (void)setVout:(vout_thread_t *)_p_vout
906 - (vout_thread_t *)getVout
911 - (void)scaleWindowWithFactor: (float)factor
914 int i_corrected_height, i_corrected_width;
916 NSPoint topleftscreen;
918 if ( !p_vout->b_fullscreen )
921 topleftbase.y = [self frame].size.height;
922 topleftscreen = [self convertBaseToScreen: topleftbase];
924 if( p_vout->output.i_height * p_vout->output.i_aspect >
925 p_vout->output.i_width * VOUT_ASPECT_FACTOR )
927 i_corrected_width = p_vout->output.i_height * p_vout->output.i_aspect /
929 newsize.width = (int) ( i_corrected_width * factor );
930 newsize.height = (int) ( p_vout->render.i_height * factor );
934 i_corrected_height = p_vout->output.i_width * VOUT_ASPECT_FACTOR /
935 p_vout->output.i_aspect;
936 newsize.width = (int) ( p_vout->render.i_width * factor );
937 newsize.height = (int) ( i_corrected_height * factor );
940 [self setContentSize: newsize];
942 [self setFrameTopLeftPoint: topleftscreen];
943 p_vout->i_changes |= VOUT_SIZE_CHANGE;
947 - (void)toggleFloatOnTop
949 if( config_GetInt( p_vout, "macosx-float" ) )
951 config_PutInt( p_vout, "macosx-float", 0 );
952 [p_vout->p_sys->o_window setLevel: NSNormalWindowLevel];
956 config_PutInt( p_vout, "macosx-float", 1 );
957 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
961 - (void)toggleFullscreen
963 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
968 return( p_vout->b_fullscreen );
971 - (BOOL)canBecomeKeyWindow
976 - (void)keyDown:(NSEvent *)o_event
981 if( [[o_event characters] length] )
983 key = [[o_event characters] characterAtIndex: 0];
989 [self toggleFullscreen];
992 case (unichar)0x1b: /* escape */
993 if( [self isFullscreen] )
995 [self toggleFullscreen];
1000 p_vout->p_vlc->b_die = VLC_TRUE;
1004 input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
1007 case (unichar)0xf700: /* arrow up */
1008 val.psz_string = "UP";
1009 var_Set( p_vout, "key-pressed", val );
1012 case (unichar)0xf701: /* arrow down */
1013 val.psz_string = "DOWN";
1014 var_Set( p_vout, "key-pressed", val );
1017 case (unichar)0xf702: /* arrow left */
1018 val.psz_string = "LEFT";
1019 var_Set( p_vout, "key-pressed", val );
1022 case (unichar)0xf703: /* arrow right */
1023 val.psz_string = "RIGHT";
1024 var_Set( p_vout, "key-pressed", val );
1027 case (unichar)0xd: /* return */
1028 case (unichar)0x3: /* enter */
1029 val.psz_string = "ENTER";
1030 var_Set( p_vout, "key-pressed", val );
1035 [super keyDown: o_event];
1042 NSMutableString * o_title;
1043 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1046 if( p_playlist == NULL )
1051 vlc_mutex_lock( &p_playlist->object_lock );
1052 o_title = [NSMutableString stringWithUTF8String:
1053 p_playlist->pp_items[p_playlist->i_index]->psz_name];
1054 vlc_mutex_unlock( &p_playlist->object_lock );
1056 vlc_object_release( p_playlist );
1058 if( o_title != nil )
1060 NSRange prefix_range = [o_title rangeOfString: @"file:"];
1061 if( prefix_range.location != NSNotFound )
1063 [o_title deleteCharactersInRange: prefix_range];
1066 [self setTitleWithRepresentedFilename: o_title];
1071 [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
1075 /* This is actually the same as VLCControls::stop. */
1076 - (BOOL)windowShouldClose:(id)sender
1078 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1080 if( p_playlist == NULL )
1085 playlist_Stop( p_playlist );
1086 vlc_object_release( p_playlist );
1088 /* The window will be closed by the intf later. */
1094 /*****************************************************************************
1095 * VLCView implementation
1096 *****************************************************************************/
1097 @implementation VLCView
1099 - (void)drawRect:(NSRect)rect
1101 vout_thread_t * p_vout;
1102 id o_window = [self window];
1103 p_vout = (vout_thread_t *)[o_window getVout];
1105 [[NSColor blackColor] set];
1107 [super drawRect: rect];
1109 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1112 - (BOOL)acceptsFirstResponder
1117 - (BOOL)becomeFirstResponder
1119 vout_thread_t * p_vout;
1120 id o_window = [self window];
1121 p_vout = (vout_thread_t *)[o_window getVout];
1123 [o_window setAcceptsMouseMovedEvents: YES];
1127 - (BOOL)resignFirstResponder
1129 vout_thread_t * p_vout;
1130 id o_window = [self window];
1131 p_vout = (vout_thread_t *)[o_window getVout];
1133 [o_window setAcceptsMouseMovedEvents: NO];
1134 VLCHideMouse( p_vout, NO );
1138 - (void)mouseDown:(NSEvent *)o_event
1140 vout_thread_t * p_vout;
1141 id o_window = [self window];
1142 p_vout = (vout_thread_t *)[o_window getVout];
1145 switch( [o_event type] )
1147 case NSLeftMouseDown:
1149 var_Get( p_vout, "mouse-button-down", &val );
1151 var_Set( p_vout, "mouse-button-down", val );
1156 [super mouseDown: o_event];
1161 - (void)otherMouseDown:(NSEvent *)o_event
1163 /* This is not the the wheel button. you need to poll the
1164 * mouseWheel event for that. other is a third, forth or fifth button */
1165 vout_thread_t * p_vout;
1166 id o_window = [self window];
1167 p_vout = (vout_thread_t *)[o_window getVout];
1170 switch( [o_event type] )
1172 case NSOtherMouseDown:
1174 var_Get( p_vout, "mouse-button-down", &val );
1176 var_Set( p_vout, "mouse-button-down", val );
1181 [super mouseDown: o_event];
1186 - (void)rightMouseDown:(NSEvent *)o_event
1188 vout_thread_t * p_vout;
1189 id o_window = [self window];
1190 p_vout = (vout_thread_t *)[o_window getVout];
1193 switch( [o_event type] )
1195 case NSRightMouseDown:
1197 var_Get( p_vout, "mouse-button-down", &val );
1199 var_Set( p_vout, "mouse-button-down", val );
1204 [super mouseDown: o_event];
1209 - (void)mouseUp:(NSEvent *)o_event
1211 vout_thread_t * p_vout;
1212 id o_window = [self window];
1213 p_vout = (vout_thread_t *)[o_window getVout];
1216 switch( [o_event type] )
1221 b_val.b_bool = VLC_TRUE;
1222 var_Set( p_vout, "mouse-clicked", b_val );
1224 var_Get( p_vout, "mouse-button-down", &val );
1226 var_Set( p_vout, "mouse-button-down", val );
1231 [super mouseUp: o_event];
1236 - (void)otherMouseUp:(NSEvent *)o_event
1238 vout_thread_t * p_vout;
1239 id o_window = [self window];
1240 p_vout = (vout_thread_t *)[o_window getVout];
1243 switch( [o_event type] )
1245 case NSOtherMouseUp:
1247 var_Get( p_vout, "mouse-button-down", &val );
1249 var_Set( p_vout, "mouse-button-down", val );
1254 [super mouseUp: o_event];
1259 - (void)rightMouseUp:(NSEvent *)o_event
1261 vout_thread_t * p_vout;
1262 id o_window = [self window];
1263 p_vout = (vout_thread_t *)[o_window getVout];
1266 switch( [o_event type] )
1268 case NSRightMouseUp:
1270 var_Get( p_vout, "mouse-button-down", &val );
1272 var_Set( p_vout, "mouse-button-down", val );
1277 [super mouseUp: o_event];
1282 - (void)mouseDragged:(NSEvent *)o_event
1284 [self mouseMoved:o_event];
1287 - (void)otherMouseDragged:(NSEvent *)o_event
1289 [self mouseMoved:o_event];
1292 - (void)rightMouseDragged:(NSEvent *)o_event
1294 [self mouseMoved:o_event];
1297 - (void)mouseMoved:(NSEvent *)o_event
1303 vout_thread_t * p_vout;
1304 id o_window = [self window];
1305 p_vout = (vout_thread_t *)[o_window getVout];
1307 s_rect = [self bounds];
1308 ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1309 b_inside = [self mouse: ml inRect: s_rect];
1314 int i_width, i_height, i_x, i_y;
1316 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1317 (unsigned int)s_rect.size.height,
1318 &i_x, &i_y, &i_width, &i_height );
1320 val.i_int = ( ((int)ml.x) - i_x ) *
1321 p_vout->render.i_width / i_width;
1322 var_Set( p_vout, "mouse-x", val );
1324 val.i_int = ( ((int)ml.y) - i_y ) *
1325 p_vout->render.i_height / i_height;
1326 var_Set( p_vout, "mouse-y", val );
1328 val.b_bool = VLC_TRUE;
1329 var_Set( p_vout, "mouse-moved", val );
1330 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1331 p_vout->p_sys->b_mouse_moved = YES;
1333 else if ( !b_inside && !p_vout->p_sys->b_mouse_pointer_visible )
1335 /* people with multiple monitors need their mouse,
1336 * even if VLCView in fullscreen. */
1337 VLCHideMouse( p_vout, NO );
1340 [super mouseMoved: o_event];
1345 /*****************************************************************************
1346 * VLCVout implementation
1347 *****************************************************************************/
1348 @implementation VLCVout
1350 - (void)createWindow:(NSValue *)o_value
1354 NSScreen * o_screen;
1355 vout_thread_t * p_vout;
1356 vlc_bool_t b_main_screen;
1358 p_vout = (vout_thread_t *)[o_value pointerValue];
1360 p_vout->p_sys->o_window = [VLCWindow alloc];
1361 [p_vout->p_sys->o_window setVout: p_vout];
1362 [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1364 if( var_Get( p_vout, "video-device", &val ) < 0 )
1366 o_screen = [NSScreen mainScreen];
1371 NSArray *o_screens = [NSScreen screens];
1372 unsigned int i_index = val.i_int;
1374 if( [o_screens count] < i_index )
1376 o_screen = [NSScreen mainScreen];
1382 o_screen = [o_screens objectAtIndex: i_index];
1383 config_PutInt( p_vout, "macosx-vdev", i_index );
1384 b_main_screen = (i_index == 0);
1388 if( p_vout->b_fullscreen )
1390 NSRect screen_rect = [o_screen frame];
1391 screen_rect.origin.x = screen_rect.origin.y = 0;
1393 if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1394 BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1395 NULL, NULL, fullScreenAllowEvents );
1397 [p_vout->p_sys->o_window
1398 initWithContentRect: screen_rect
1399 styleMask: NSBorderlessWindowMask
1400 backing: NSBackingStoreBuffered
1401 defer: NO screen: o_screen];
1403 [p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1404 p_vout->p_sys->b_mouse_moved = YES;
1405 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1409 unsigned int i_stylemask = NSTitledWindowMask |
1410 NSMiniaturizableWindowMask |
1411 NSClosableWindowMask |
1412 NSResizableWindowMask;
1414 if ( p_vout->p_sys->p_fullscreen_state != NULL )
1415 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1416 p_vout->p_sys->p_fullscreen_state = NULL;
1418 [p_vout->p_sys->o_window
1419 initWithContentRect: p_vout->p_sys->s_rect
1420 styleMask: i_stylemask
1421 backing: NSBackingStoreBuffered
1422 defer: NO screen: o_screen];
1424 [p_vout->p_sys->o_window setAlphaValue: config_GetFloat( p_vout, "macosx-opaqueness" )];
1426 if( config_GetInt( p_vout, "macosx-float" ) )
1428 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
1431 if( !p_vout->p_sys->b_pos_saved )
1433 [p_vout->p_sys->o_window center];
1437 o_view = [[VLCView alloc] init];
1439 /* FIXME: [o_view setMenu:] */
1440 [p_vout->p_sys->o_window setContentView: o_view];
1441 [o_view autorelease];
1444 p_vout->p_sys->p_qdport = [o_view qdPort];
1446 [o_view unlockFocus];
1448 [p_vout->p_sys->o_window updateTitle];
1449 [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1452 - (void)destroyWindow:(NSValue *)o_value
1454 vout_thread_t * p_vout;
1456 p_vout = (vout_thread_t *)[o_value pointerValue];
1458 if( !p_vout->b_fullscreen )
1462 s_rect = [[p_vout->p_sys->o_window contentView] frame];
1463 p_vout->p_sys->s_rect.size = s_rect.size;
1465 s_rect = [p_vout->p_sys->o_window frame];
1466 p_vout->p_sys->s_rect.origin = s_rect.origin;
1468 p_vout->p_sys->b_pos_saved = YES;
1471 p_vout->p_sys->p_qdport = nil;
1472 [p_vout->p_sys->o_window close];
1473 p_vout->p_sys->o_window = nil;