2 /*****************************************************************************
3 * vout.m: MacOS X video output plugin
4 *****************************************************************************
5 * Copyright (C) 2001-2003 VideoLAN
6 * $Id: vout.m,v 1.64 2003/11/11 23:50:41 hartman Exp $
8 * Authors: Colin Delacroix <colin@zoy.org>
9 * Florian G. Pflug <fgp@phlo.org>
10 * Jon Lech Johansen <jon-vl@nanocrew.net>
11 * Derk-Jan Hartman <thedj@users.sourceforge.net>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
26 *****************************************************************************/
28 /*****************************************************************************
30 *****************************************************************************/
31 #include <errno.h> /* ENOMEM */
32 #include <stdlib.h> /* free() */
33 #include <string.h> /* strerror() */
35 #include <QuickTime/QuickTime.h>
42 #define QT_MAX_DIRECTBUFFERS 10
43 #define VL_MAX_DISPLAYS 16
50 /* When using I420 output */
51 PlanarPixmapInfoYUV420 pixmap_i420;
54 /*****************************************************************************
56 *****************************************************************************/
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 VLCHideMouse ( vout_thread_t *, BOOL );
70 static void QTScaleMatrix ( vout_thread_t * );
71 static int QTCreateSequence ( vout_thread_t * );
72 static void QTDestroySequence ( vout_thread_t * );
73 static int QTNewPicture ( vout_thread_t *, picture_t * );
74 static void QTFreePicture ( vout_thread_t *, picture_t * );
76 /*****************************************************************************
77 * OpenVideo: allocates MacOS X video thread output method
78 *****************************************************************************
79 * This function allocates and initializes a MacOS X vout method.
80 *****************************************************************************/
81 int E_(OpenVideo) ( vlc_object_t *p_this )
83 vout_thread_t * p_vout = (vout_thread_t *)p_this;
86 vlc_value_t value_drawable;
88 var_Get( p_vout->p_vlc, "drawable", &value_drawable );
90 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
91 if( p_vout->p_sys == NULL )
93 msg_Err( p_vout, "out of memory" );
97 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
99 /* We don't need an intf in mozilla plugin */
100 if( value_drawable.i_int == 0 )
102 /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
103 for( i_timeout = 20 ; i_timeout-- ; )
107 msleep( INTF_IDLE_SLEEP );
113 /* no MacOS X intf, unable to communicate with MT */
114 msg_Err( p_vout, "no MacOS X interface present" );
115 free( p_vout->p_sys );
120 if( [NSApp respondsToSelector: @selector(getIntf)] )
122 intf_thread_t * p_intf;
124 for( i_timeout = 10 ; i_timeout-- ; )
126 if( ( p_intf = [NSApp getIntf] ) == NULL )
128 msleep( INTF_IDLE_SLEEP );
134 msg_Err( p_vout, "MacOS X intf has getIntf, but is NULL" );
135 free( p_vout->p_sys );
141 p_vout->p_sys->h_img_descr =
142 (ImageDescriptionHandle)NewHandleClear( sizeof(ImageDescription) );
143 p_vout->p_sys->p_matrix = (MatrixRecordPtr)malloc( sizeof(MatrixRecord) );
144 p_vout->p_sys->p_fullscreen_state = NULL;
146 p_vout->p_sys->b_mouse_moved = VLC_TRUE;
147 p_vout->p_sys->i_time_mouse_last_moved = mdate();
149 if( value_drawable.i_int != 0 )
151 p_vout->p_sys->mask = NewRgn();
152 p_vout->p_sys->rect.left = 0 ;
153 p_vout->p_sys->rect.right = 0 ;
154 p_vout->p_sys->rect.top = 0 ;
155 p_vout->p_sys->rect.bottom = 0 ;
157 p_vout->p_sys->isplugin = VLC_TRUE ;
161 p_vout->p_sys->mask = NULL;
162 p_vout->p_sys->isplugin = VLC_FALSE ;
165 /* set window size */
166 p_vout->p_sys->s_rect.size.width = p_vout->i_window_width;
167 p_vout->p_sys->s_rect.size.height = p_vout->i_window_height;
169 if( ( err = EnterMovies() ) != noErr )
171 msg_Err( p_vout, "EnterMovies failed: %d", err );
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 /* Damn QT isn't thread safe. so keep a lock in the p_vlc object */
179 vlc_mutex_lock( &p_vout->p_vlc->quicktime_lock );
181 err = FindCodec( kYUV420CodecType, bestSpeedCodec,
182 nil, &p_vout->p_sys->img_dc );
184 vlc_mutex_unlock( &p_vout->p_vlc->quicktime_lock );
185 if( err == noErr && p_vout->p_sys->img_dc != 0 )
187 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
188 p_vout->p_sys->i_codec = kYUV420CodecType;
192 msg_Err( p_vout, "failed to find an appropriate codec" );
195 if( p_vout->p_sys->img_dc == 0 )
197 free( p_vout->p_sys->p_matrix );
198 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
199 free( p_vout->p_sys );
203 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
204 NSArray * o_screens = [NSScreen screens];
205 if( [o_screens count] > 0 && var_Type( p_vout, "video-device" ) == 0 )
208 vlc_value_t val, text;
211 int i_option = config_GetInt( p_vout, "macosx-vdev" );
213 var_Create( p_vout, "video-device", VLC_VAR_INTEGER |
215 text.psz_string = _("Video device");
216 var_Change( p_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL );
218 NSEnumerator * o_enumerator = [o_screens objectEnumerator];
220 while( (o_screen = [o_enumerator nextObject]) != NULL )
223 NSRect s_rect = [o_screen frame];
225 snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
226 "%s %d (%dx%d)", _("Screen"), i,
227 (int)s_rect.size.width, (int)s_rect.size.height );
229 text.psz_string = psz_temp;
231 var_Change( p_vout, "video-device",
232 VLC_VAR_ADDCHOICE, &val, &text );
234 if( ( i - 1 ) == i_option )
236 var_Set( p_vout, "video-device", val );
241 var_AddCallback( p_vout, "video-device", vout_VarCallback,
244 val.b_bool = VLC_TRUE;
245 var_Set( p_vout, "intf-change", val );
249 /* We don't need a window either in the mozilla plugin */
250 if( p_vout->p_sys->isplugin == 0 )
252 if( CoCreateWindow( p_vout ) )
254 msg_Err( p_vout, "unable to create window" );
255 free( p_vout->p_sys->p_matrix );
256 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
257 free( p_vout->p_sys );
262 p_vout->pf_init = vout_Init;
263 p_vout->pf_end = vout_End;
264 p_vout->pf_manage = vout_Manage;
265 p_vout->pf_render = NULL;
266 p_vout->pf_display = vout_Display;
271 /*****************************************************************************
272 * vout_Init: initialize video thread output method
273 *****************************************************************************/
274 static int vout_Init( vout_thread_t *p_vout )
280 I_OUTPUTPICTURES = 0;
282 /* Initialize the output structure; we already found a codec,
283 * and the corresponding chroma we will be using. Since we can
284 * arbitrary scale, stick to the coordinates and aspect. */
285 p_vout->output.i_width = p_vout->render.i_width;
286 p_vout->output.i_height = p_vout->render.i_height;
287 p_vout->output.i_aspect = p_vout->render.i_aspect;
289 var_Get( p_vout->p_vlc, "drawable", &val );
291 if( p_vout->p_sys->isplugin )
293 p_vout->p_sys->p_qdport = val.i_int;
296 SetPort( p_vout->p_sys->p_qdport );
297 QTScaleMatrix( p_vout );
299 if( QTCreateSequence( p_vout ) )
301 msg_Err( p_vout, "unable to create sequence" );
305 /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
306 while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
310 /* Find an empty picture slot */
311 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
313 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
315 p_pic = p_vout->p_picture + i_index;
320 /* Allocate the picture */
321 if( p_pic == NULL || QTNewPicture( p_vout, p_pic ) )
326 p_pic->i_status = DESTROYED_PICTURE;
327 p_pic->i_type = DIRECT_PICTURE;
329 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
337 /*****************************************************************************
338 * vout_End: terminate video thread output method
339 *****************************************************************************/
340 static void vout_End( vout_thread_t *p_vout )
344 QTDestroySequence( p_vout );
346 /* Free the direct buffers we allocated */
347 for( i_index = I_OUTPUTPICTURES; i_index; )
350 QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
354 /*****************************************************************************
355 * CloseVideo: destroy video thread output method
356 *****************************************************************************/
357 void E_(CloseVideo) ( vlc_object_t *p_this )
359 vout_thread_t * p_vout = (vout_thread_t *)p_this;
361 if ( !p_vout->p_sys->isplugin )
363 if( CoDestroyWindow( p_vout ) )
365 msg_Err( p_vout, "unable to destroy window" );
369 if ( p_vout->p_sys->p_fullscreen_state != NULL )
370 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
374 free( p_vout->p_sys->p_matrix );
375 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
377 free( p_vout->p_sys );
380 /*****************************************************************************
381 * vout_Manage: handle events
382 *****************************************************************************
383 * This function should be called regularly by video output thread. It manages
384 * console events. It returns a non null value on error.
385 *****************************************************************************/
386 static int vout_Manage( vout_thread_t *p_vout )
389 var_Get( p_vout->p_vlc, "drawableredraw", &val1 );
391 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
393 if( CoToggleFullscreen( p_vout ) )
398 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
401 if( (p_vout->i_changes & VOUT_SIZE_CHANGE) ||
402 ( p_vout->p_sys->isplugin && val1.i_int == 1) )
404 if( p_vout->p_sys->isplugin )
407 var_Set( p_vout->p_vlc, "drawableredraw", val1 );
408 QTScaleMatrix( p_vout );
409 SetDSequenceMask( p_vout->p_sys->i_seq , p_vout->p_sys->mask );
413 QTScaleMatrix( p_vout );
414 SetDSequenceMatrix( p_vout->p_sys->i_seq,
415 p_vout->p_sys->p_matrix );
416 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
420 /* hide/show mouse cursor
421 * this code looks unnecessarily complicated, but is necessary like this.
422 * it has to deal with multiple monitors and therefore checks a lot */
423 if( !p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
425 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 3000000 )
427 VLCHideMouse( p_vout, YES );
430 else if ( p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
432 VLCHideMouse( p_vout, NO );
438 /*****************************************************************************
439 * vout_Display: displays previously rendered output
440 *****************************************************************************
441 * This function sends the currently rendered image to the display.
442 *****************************************************************************/
443 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
450 if( p_vout->p_sys->isplugin )
454 /* In mozilla plugin, mozilla browser also draws things in
455 * the windows. So we have to update the port/Origin for each
456 * picture. FIXME : the vout should lock something ! */
457 GetPort( &p_vout->p_sys->p_qdportold );
458 GetPortBounds( p_vout->p_sys->p_qdportold, &oldrect );
461 LockPortBits( p_vout->p_sys->p_qdport );
463 SetPort( p_vout->p_sys->p_qdport );
464 SetOrigin( p_vout->p_sys->portx , p_vout->p_sys->porty );
465 ClipRect( &p_vout->p_sys->rect );
468 if( ( err = DecompressSequenceFrameS(
469 p_vout->p_sys->i_seq,
470 p_pic->p_sys->p_info,
471 p_pic->p_sys->i_size,
472 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
474 msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
478 QDFlushPortBuffer( p_vout->p_sys->p_qdport, p_vout->p_sys->mask );
482 SetOrigin( oldrect.left , oldrect.top );
484 SetPort( p_vout->p_sys->p_qdportold );
486 UnlockPortBits( p_vout->p_sys->p_qdport );
491 if( ( err = DecompressSequenceFrameS(
492 p_vout->p_sys->i_seq,
493 p_pic->p_sys->p_info,
494 p_pic->p_sys->i_size,
495 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
497 msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
501 QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
506 /*****************************************************************************
507 * CoSendRequest: send request to interface thread
508 *****************************************************************************
509 * Returns 0 on success, 1 otherwise
510 *****************************************************************************/
511 static int CoSendRequest( vout_thread_t *p_vout, SEL sel )
515 VLCVout * o_vlv = [[VLCVout alloc] init];
517 if( ( i_ret = ExecuteOnMainThread( o_vlv, sel, (void *)p_vout ) ) )
519 msg_Err( p_vout, "SendRequest: no way to communicate with mt" );
527 /*****************************************************************************
528 * CoCreateWindow: create new window
529 *****************************************************************************
530 * Returns 0 on success, 1 otherwise
531 *****************************************************************************/
532 static int CoCreateWindow( vout_thread_t *p_vout )
534 if( CoSendRequest( p_vout, @selector(createWindow:) ) )
536 msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
543 /*****************************************************************************
544 * CoDestroyWindow: destroy window
545 *****************************************************************************
546 * Returns 0 on success, 1 otherwise
547 *****************************************************************************/
548 static int CoDestroyWindow( vout_thread_t *p_vout )
550 if( [[NSCursor currentCursor] image] == NULL )
552 VLCHideMouse( p_vout, NO );
555 if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
557 msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
564 /*****************************************************************************
565 * CoToggleFullscreen: toggle fullscreen
566 *****************************************************************************
567 * Returns 0 on success, 1 otherwise
568 *****************************************************************************/
569 static int CoToggleFullscreen( vout_thread_t *p_vout )
571 QTDestroySequence( p_vout );
573 if( CoDestroyWindow( p_vout ) )
575 msg_Err( p_vout, "unable to destroy window" );
579 p_vout->b_fullscreen = !p_vout->b_fullscreen;
581 config_PutInt( p_vout, "fullscreen", p_vout->b_fullscreen );
583 if( CoCreateWindow( p_vout ) )
585 msg_Err( p_vout, "unable to create window" );
589 if( QTCreateSequence( p_vout ) )
591 msg_Err( p_vout, "unable to create sequence" );
598 /*****************************************************************************
599 * VLCHideMouse: if b_hide then hide the cursor
600 *****************************************************************************/
601 static void VLCHideMouse ( vout_thread_t *p_vout, BOOL b_hide )
606 NSWindow *o_window = p_vout->p_sys->o_window;
607 NSView *o_contents = [o_window contentView];
609 s_rect = [o_contents bounds];
610 ml = [o_window convertScreenToBase:[NSEvent mouseLocation]];
611 ml = [o_contents convertPoint:ml fromView:nil];
612 b_inside = [o_contents mouse: ml inRect: s_rect];
614 if ( b_hide && b_inside )
616 /* only hide if mouse over VLCView */
617 [NSCursor setHiddenUntilMouseMoves: YES];
621 [NSCursor setHiddenUntilMouseMoves: NO];
623 p_vout->p_sys->b_mouse_moved = NO;
624 p_vout->p_sys->i_time_mouse_last_moved = mdate();
628 /*****************************************************************************
629 * QTScaleMatrix: scale matrix
630 *****************************************************************************/
631 static void QTScaleMatrix( vout_thread_t *p_vout )
634 unsigned int i_width, i_height;
635 Fixed factor_x, factor_y;
636 unsigned int i_offset_x = 0;
637 unsigned int i_offset_y = 0;
647 vlc_value_t valportx;
648 vlc_value_t valporty;
650 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
651 i_width = s_rect.right - s_rect.left;
652 i_height = s_rect.bottom - s_rect.top;
654 var_Get( p_vout->p_vlc, "drawable", &val );
655 var_Get( p_vout->p_vlc, "drawablet", &valt );
656 var_Get( p_vout->p_vlc, "drawablel", &vall );
657 var_Get( p_vout->p_vlc, "drawableb", &valb );
658 var_Get( p_vout->p_vlc, "drawabler", &valr );
659 var_Get( p_vout->p_vlc, "drawablex", &valx );
660 var_Get( p_vout->p_vlc, "drawabley", &valy );
661 var_Get( p_vout->p_vlc, "drawablew", &valw );
662 var_Get( p_vout->p_vlc, "drawableh", &valh );
663 var_Get( p_vout->p_vlc, "drawableportx", &valportx );
664 var_Get( p_vout->p_vlc, "drawableporty", &valporty );
666 if( p_vout->p_sys->isplugin )
668 p_vout->p_sys->portx = valportx.i_int;
669 p_vout->p_sys->porty = valporty.i_int;
670 p_vout->p_sys->p_qdport = val.i_int;
671 i_width = valw.i_int;
672 i_height = valh.i_int;
674 SetRectRgn( p_vout->p_sys->mask , vall.i_int - valx.i_int ,
675 valt.i_int - valy.i_int , valr.i_int - valx.i_int ,
676 valb.i_int - valy.i_int );
677 p_vout->p_sys->rect.top = 0;
678 p_vout->p_sys->rect.left = 0;
679 p_vout->p_sys->rect.bottom = valb.i_int - valt.i_int;
680 p_vout->p_sys->rect.right = valr.i_int - vall.i_int;
683 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
685 int i_adj_width = i_height * p_vout->output.i_aspect /
688 factor_x = FixDiv( Long2Fix( i_adj_width ),
689 Long2Fix( p_vout->output.i_width ) );
690 factor_y = FixDiv( Long2Fix( i_height ),
691 Long2Fix( p_vout->output.i_height ) );
693 i_offset_x = (i_width - i_adj_width) / 2 + i_offset_x;
697 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
698 p_vout->output.i_aspect;
700 factor_x = FixDiv( Long2Fix( i_width ),
701 Long2Fix( p_vout->output.i_width ) );
702 factor_y = FixDiv( Long2Fix( i_adj_height ),
703 Long2Fix( p_vout->output.i_height ) );
705 i_offset_y = (i_height - i_adj_height) / 2 + i_offset_y;
708 SetIdentityMatrix( p_vout->p_sys->p_matrix );
710 ScaleMatrix( p_vout->p_sys->p_matrix,
712 Long2Fix(0), Long2Fix(0) );
714 TranslateMatrix( p_vout->p_sys->p_matrix,
715 Long2Fix(i_offset_x),
716 Long2Fix(i_offset_y) );
720 /*****************************************************************************
721 * QTCreateSequence: create a new sequence
722 *****************************************************************************
723 * Returns 0 on success, 1 otherwise
724 *****************************************************************************/
725 static int QTCreateSequence( vout_thread_t *p_vout )
728 ImageDescriptionPtr p_descr;
730 HLock( (Handle)p_vout->p_sys->h_img_descr );
731 p_descr = *p_vout->p_sys->h_img_descr;
733 p_descr->idSize = sizeof(ImageDescription);
734 p_descr->cType = p_vout->p_sys->i_codec;
735 p_descr->version = 1;
736 p_descr->revisionLevel = 0;
737 p_descr->vendor = 'appl';
738 p_descr->width = p_vout->output.i_width;
739 p_descr->height = p_vout->output.i_height;
740 p_descr->hRes = Long2Fix(72);
741 p_descr->vRes = Long2Fix(72);
742 p_descr->spatialQuality = codecLosslessQuality;
743 p_descr->frameCount = 1;
744 p_descr->clutID = -1;
745 p_descr->dataSize = 0;
749 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
752 if( ( err = DecompressSequenceBeginS(
753 &p_vout->p_sys->i_seq,
754 p_vout->p_sys->h_img_descr,
756 p_vout->p_sys->p_qdport,
758 p_vout->p_sys->p_matrix,
759 0, p_vout->p_sys->mask,
760 codecFlagUseImageBuffer,
761 codecLosslessQuality,
762 p_vout->p_sys->img_dc ) ) )
764 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
772 /*****************************************************************************
773 * QTDestroySequence: destroy sequence
774 *****************************************************************************/
775 static void QTDestroySequence( vout_thread_t *p_vout )
777 CDSequenceEnd( p_vout->p_sys->i_seq );
780 /*****************************************************************************
781 * QTNewPicture: allocate a picture
782 *****************************************************************************
783 * Returns 0 on success, 1 otherwise
784 *****************************************************************************/
785 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
787 int i_width = p_vout->output.i_width;
788 int i_height = p_vout->output.i_height;
790 /* We know the chroma, allocate a buffer which will be used
791 * directly by the decoder */
792 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
794 if( p_pic->p_sys == NULL )
799 switch( p_vout->output.i_chroma )
801 case VLC_FOURCC('I','4','2','0'):
803 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
804 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
806 /* Allocate the memory buffer */
807 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
808 16, i_width * i_height * 3 / 2 );
811 p_pic->Y_PIXELS = p_pic->p_data;
812 p_pic->p[Y_PLANE].i_lines = i_height;
813 p_pic->p[Y_PLANE].i_pitch = i_width;
814 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
815 p_pic->p[Y_PLANE].i_visible_pitch = i_width;
818 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
819 p_pic->p[U_PLANE].i_lines = i_height / 2;
820 p_pic->p[U_PLANE].i_pitch = i_width / 2;
821 p_pic->p[U_PLANE].i_pixel_pitch = 1;
822 p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
825 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
826 p_pic->p[V_PLANE].i_lines = i_height / 2;
827 p_pic->p[V_PLANE].i_pitch = i_width / 2;
828 p_pic->p[V_PLANE].i_pixel_pitch = 1;
829 p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
831 /* We allocated 3 planes */
834 #define P p_pic->p_sys->pixmap_i420
835 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
836 - p_pic->p_sys->p_info;
837 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
838 - p_pic->p_sys->p_info;
839 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
840 - p_pic->p_sys->p_info;
842 P.componentInfoY.rowBytes = i_width;
843 P.componentInfoCb.rowBytes = i_width / 2;
844 P.componentInfoCr.rowBytes = i_width / 2;
850 /* Unknown chroma, tell the guy to get lost */
851 free( p_pic->p_sys );
852 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
853 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
861 /*****************************************************************************
862 * QTFreePicture: destroy a picture allocated with QTNewPicture
863 *****************************************************************************/
864 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
866 switch( p_vout->output.i_chroma )
868 case VLC_FOURCC('I','4','2','0'):
869 free( p_pic->p_data_orig );
873 free( p_pic->p_sys );
876 /*****************************************************************************
877 * VLCWindow implementation
878 *****************************************************************************/
879 @implementation VLCWindow
881 - (void)setVout:(vout_thread_t *)_p_vout
886 - (vout_thread_t *)getVout
891 - (void)scaleWindowWithFactor: (float)factor
894 int i_corrected_height, i_corrected_width;
896 NSPoint topleftscreen;
898 if ( !p_vout->b_fullscreen )
901 topleftbase.y = [self frame].size.height;
902 topleftscreen = [self convertBaseToScreen: topleftbase];
904 if( p_vout->output.i_height * p_vout->output.i_aspect >
905 p_vout->output.i_width * VOUT_ASPECT_FACTOR )
907 i_corrected_width = p_vout->output.i_height * p_vout->output.i_aspect /
909 newsize.width = (int) ( i_corrected_width * factor );
910 newsize.height = (int) ( p_vout->render.i_height * factor );
914 i_corrected_height = p_vout->output.i_width * VOUT_ASPECT_FACTOR /
915 p_vout->output.i_aspect;
916 newsize.width = (int) ( p_vout->render.i_width * factor );
917 newsize.height = (int) ( i_corrected_height * factor );
920 [self setContentSize: newsize];
922 [self setFrameTopLeftPoint: topleftscreen];
923 p_vout->i_changes |= VOUT_SIZE_CHANGE;
927 - (void)toggleFloatOnTop
929 if( config_GetInt( p_vout, "macosx-float" ) )
931 config_PutInt( p_vout, "macosx-float", 0 );
932 [p_vout->p_sys->o_window setLevel: NSNormalWindowLevel];
936 config_PutInt( p_vout, "macosx-float", 1 );
937 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
941 - (void)toggleFullscreen
943 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
948 return( p_vout->b_fullscreen );
951 - (BOOL)canBecomeKeyWindow
956 - (void)keyDown:(NSEvent *)o_event
960 unsigned int i_pressed_modifiers = 0;
963 i_pressed_modifiers = [o_event modifierFlags];
965 if( i_pressed_modifiers & NSShiftKeyMask )
966 val.i_int |= KEY_MODIFIER_SHIFT;
967 if( i_pressed_modifiers & NSControlKeyMask )
968 val.i_int |= KEY_MODIFIER_CTRL;
969 if( i_pressed_modifiers & NSAlternateKeyMask )
970 val.i_int |= KEY_MODIFIER_ALT;
971 if( i_pressed_modifiers & NSCommandKeyMask )
972 val.i_int |= KEY_MODIFIER_COMMAND;
974 key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
978 /* Escape should always get you out of fullscreen */
979 if( key == (unichar) 0x1b )
981 if( [self isFullscreen] )
983 [self toggleFullscreen];
986 else if ( key == ' ' )
988 playlist_t *p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
990 if ( p_playlist != NULL )
992 playlist_Pause( p_playlist );
993 vlc_object_release( p_playlist);
998 val.i_int |= CocoaKeyToVLC( key );
999 var_Set( p_vout->p_vlc, "key-pressed", val );
1004 [super keyDown: o_event];
1010 NSMutableString * o_title;
1011 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1014 if( p_playlist == NULL )
1019 vlc_mutex_lock( &p_playlist->object_lock );
1020 o_title = [NSMutableString stringWithUTF8String:
1021 p_playlist->pp_items[p_playlist->i_index]->psz_uri];
1022 vlc_mutex_unlock( &p_playlist->object_lock );
1024 vlc_object_release( p_playlist );
1026 if( o_title != nil )
1028 NSRange prefix_range = [o_title rangeOfString: @"file:"];
1029 if( prefix_range.location != NSNotFound )
1031 [o_title deleteCharactersInRange: prefix_range];
1034 [self setTitleWithRepresentedFilename: o_title];
1039 [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
1043 /* This is actually the same as VLCControls::stop. */
1044 - (BOOL)windowShouldClose:(id)sender
1046 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1048 if( p_playlist == NULL )
1053 playlist_Stop( p_playlist );
1054 vlc_object_release( p_playlist );
1056 /* The window will be closed by the intf later. */
1062 /*****************************************************************************
1063 * VLCView implementation
1064 *****************************************************************************/
1065 @implementation VLCView
1067 - (void)drawRect:(NSRect)rect
1069 vout_thread_t * p_vout;
1070 id o_window = [self window];
1071 p_vout = (vout_thread_t *)[o_window getVout];
1073 [[NSColor blackColor] set];
1075 [super drawRect: rect];
1077 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1080 - (BOOL)acceptsFirstResponder
1085 - (BOOL)becomeFirstResponder
1087 vout_thread_t * p_vout;
1088 id o_window = [self window];
1089 p_vout = (vout_thread_t *)[o_window getVout];
1091 [o_window setAcceptsMouseMovedEvents: YES];
1095 - (BOOL)resignFirstResponder
1097 vout_thread_t * p_vout;
1098 id o_window = [self window];
1099 p_vout = (vout_thread_t *)[o_window getVout];
1101 [o_window setAcceptsMouseMovedEvents: NO];
1102 VLCHideMouse( p_vout, NO );
1106 - (void)mouseDown:(NSEvent *)o_event
1108 vout_thread_t * p_vout;
1109 id o_window = [self window];
1110 p_vout = (vout_thread_t *)[o_window getVout];
1113 switch( [o_event type] )
1115 case NSLeftMouseDown:
1117 var_Get( p_vout, "mouse-button-down", &val );
1119 var_Set( p_vout, "mouse-button-down", val );
1124 [super mouseDown: o_event];
1129 - (void)otherMouseDown:(NSEvent *)o_event
1131 /* This is not the the wheel button. you need to poll the
1132 * mouseWheel event for that. other is a third, forth or fifth button */
1133 vout_thread_t * p_vout;
1134 id o_window = [self window];
1135 p_vout = (vout_thread_t *)[o_window getVout];
1138 switch( [o_event type] )
1140 case NSOtherMouseDown:
1142 var_Get( p_vout, "mouse-button-down", &val );
1144 var_Set( p_vout, "mouse-button-down", val );
1149 [super mouseDown: o_event];
1154 - (void)rightMouseDown:(NSEvent *)o_event
1156 vout_thread_t * p_vout;
1157 id o_window = [self window];
1158 p_vout = (vout_thread_t *)[o_window getVout];
1161 switch( [o_event type] )
1163 case NSRightMouseDown:
1165 var_Get( p_vout, "mouse-button-down", &val );
1167 var_Set( p_vout, "mouse-button-down", val );
1172 [super mouseDown: o_event];
1177 - (void)mouseUp:(NSEvent *)o_event
1179 vout_thread_t * p_vout;
1180 id o_window = [self window];
1181 p_vout = (vout_thread_t *)[o_window getVout];
1184 switch( [o_event type] )
1189 b_val.b_bool = VLC_TRUE;
1190 var_Set( p_vout, "mouse-clicked", b_val );
1192 var_Get( p_vout, "mouse-button-down", &val );
1194 var_Set( p_vout, "mouse-button-down", val );
1199 [super mouseUp: o_event];
1204 - (void)otherMouseUp:(NSEvent *)o_event
1206 vout_thread_t * p_vout;
1207 id o_window = [self window];
1208 p_vout = (vout_thread_t *)[o_window getVout];
1211 switch( [o_event type] )
1213 case NSOtherMouseUp:
1215 var_Get( p_vout, "mouse-button-down", &val );
1217 var_Set( p_vout, "mouse-button-down", val );
1222 [super mouseUp: o_event];
1227 - (void)rightMouseUp:(NSEvent *)o_event
1229 vout_thread_t * p_vout;
1230 id o_window = [self window];
1231 p_vout = (vout_thread_t *)[o_window getVout];
1234 switch( [o_event type] )
1236 case NSRightMouseUp:
1238 var_Get( p_vout, "mouse-button-down", &val );
1240 var_Set( p_vout, "mouse-button-down", val );
1245 [super mouseUp: o_event];
1250 - (void)mouseDragged:(NSEvent *)o_event
1252 [self mouseMoved:o_event];
1255 - (void)otherMouseDragged:(NSEvent *)o_event
1257 [self mouseMoved:o_event];
1260 - (void)rightMouseDragged:(NSEvent *)o_event
1262 [self mouseMoved:o_event];
1265 - (void)mouseMoved:(NSEvent *)o_event
1271 vout_thread_t * p_vout;
1272 id o_window = [self window];
1273 p_vout = (vout_thread_t *)[o_window getVout];
1275 s_rect = [self bounds];
1276 ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1277 b_inside = [self mouse: ml inRect: s_rect];
1282 int i_width, i_height, i_x, i_y;
1284 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1285 (unsigned int)s_rect.size.height,
1286 &i_x, &i_y, &i_width, &i_height );
1288 val.i_int = ( ((int)ml.x) - i_x ) *
1289 p_vout->render.i_width / i_width;
1290 var_Set( p_vout, "mouse-x", val );
1292 val.i_int = ( ((int)ml.y) - i_y ) *
1293 p_vout->render.i_height / i_height;
1294 var_Set( p_vout, "mouse-y", val );
1296 val.b_bool = VLC_TRUE;
1297 var_Set( p_vout, "mouse-moved", val );
1298 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1299 p_vout->p_sys->b_mouse_moved = YES;
1302 [super mouseMoved: o_event];
1307 /*****************************************************************************
1308 * VLCVout implementation
1309 *****************************************************************************/
1310 @implementation VLCVout
1312 - (void)createWindow:(NSValue *)o_value
1316 NSScreen * o_screen;
1317 vout_thread_t * p_vout;
1318 vlc_bool_t b_main_screen;
1320 p_vout = (vout_thread_t *)[o_value pointerValue];
1322 p_vout->p_sys->o_window = [VLCWindow alloc];
1323 [p_vout->p_sys->o_window setVout: p_vout];
1324 [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1326 if( var_Get( p_vout, "video-device", &val ) < 0 )
1328 o_screen = [NSScreen mainScreen];
1333 NSArray *o_screens = [NSScreen screens];
1334 unsigned int i_index = val.i_int;
1336 if( [o_screens count] < i_index )
1338 o_screen = [NSScreen mainScreen];
1344 o_screen = [o_screens objectAtIndex: i_index];
1345 config_PutInt( p_vout, "macosx-vdev", i_index );
1346 b_main_screen = (i_index == 0);
1350 if( p_vout->b_fullscreen )
1352 NSRect screen_rect = [o_screen frame];
1353 screen_rect.origin.x = screen_rect.origin.y = 0;
1355 if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1356 BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1357 NULL, NULL, fullScreenAllowEvents );
1359 [p_vout->p_sys->o_window
1360 initWithContentRect: screen_rect
1361 styleMask: NSBorderlessWindowMask
1362 backing: NSBackingStoreBuffered
1363 defer: NO screen: o_screen];
1365 //[p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1366 p_vout->p_sys->b_mouse_moved = YES;
1367 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1371 unsigned int i_stylemask = NSTitledWindowMask |
1372 NSMiniaturizableWindowMask |
1373 NSClosableWindowMask |
1374 NSResizableWindowMask;
1376 if ( p_vout->p_sys->p_fullscreen_state != NULL )
1377 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1378 p_vout->p_sys->p_fullscreen_state = NULL;
1380 [p_vout->p_sys->o_window
1381 initWithContentRect: p_vout->p_sys->s_rect
1382 styleMask: i_stylemask
1383 backing: NSBackingStoreBuffered
1384 defer: NO screen: o_screen];
1386 [p_vout->p_sys->o_window setAlphaValue: config_GetFloat( p_vout, "macosx-opaqueness" )];
1388 if( config_GetInt( p_vout, "macosx-float" ) )
1390 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
1393 if( !p_vout->p_sys->b_pos_saved )
1395 [p_vout->p_sys->o_window center];
1399 o_view = [[VLCView alloc] init];
1401 /* FIXME: [o_view setMenu:] */
1402 [p_vout->p_sys->o_window setContentView: o_view];
1403 [o_view autorelease];
1406 p_vout->p_sys->p_qdport = [o_view qdPort];
1408 [o_view unlockFocus];
1410 [p_vout->p_sys->o_window updateTitle];
1411 [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1414 - (void)destroyWindow:(NSValue *)o_value
1416 vout_thread_t * p_vout;
1418 p_vout = (vout_thread_t *)[o_value pointerValue];
1420 if( !p_vout->b_fullscreen )
1424 s_rect = [[p_vout->p_sys->o_window contentView] frame];
1425 p_vout->p_sys->s_rect.size = s_rect.size;
1427 s_rect = [p_vout->p_sys->o_window frame];
1428 p_vout->p_sys->s_rect.origin = s_rect.origin;
1430 p_vout->p_sys->b_pos_saved = YES;
1433 p_vout->p_sys->p_qdport = nil;
1434 [p_vout->p_sys->o_window close];
1435 p_vout->p_sys->o_window = nil;