2 /*****************************************************************************
3 * vout.m: MacOS X video output plugin
4 *****************************************************************************
5 * Copyright (C) 2001-2003 VideoLAN
6 * $Id: vout.m,v 1.56 2003/09/20 13:46:00 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>
40 #define QT_MAX_DIRECTBUFFERS 10
41 #define VL_MAX_DISPLAYS 16
48 /* When using I420 output */
49 PlanarPixmapInfoYUV420 pixmap_i420;
52 /*****************************************************************************
54 *****************************************************************************/
56 static int vout_Init ( vout_thread_t * );
57 static void vout_End ( vout_thread_t * );
58 static int vout_Manage ( vout_thread_t * );
59 static void vout_Display ( vout_thread_t *, picture_t * );
61 static int CoSendRequest ( vout_thread_t *, SEL );
62 static int CoCreateWindow ( vout_thread_t * );
63 static int CoDestroyWindow ( vout_thread_t * );
64 static int CoToggleFullscreen ( vout_thread_t * );
66 static void VLCHideMouse ( vout_thread_t *, BOOL );
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;
84 vlc_value_t value_drawable;
86 var_Get( p_vout->p_vlc, "drawable", &value_drawable );
88 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
89 if( p_vout->p_sys == NULL )
91 msg_Err( p_vout, "out of memory" );
95 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
97 /* We don't need an intf in mozilla plugin */
98 if( value_drawable.i_int == 0 )
100 /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
101 for( i_timeout = 20 ; i_timeout-- ; )
105 msleep( INTF_IDLE_SLEEP );
111 /* no MacOS X intf, unable to communicate with MT */
112 msg_Err( p_vout, "no MacOS X interface present" );
113 free( p_vout->p_sys );
118 if( [NSApp respondsToSelector: @selector(getIntf)] )
120 intf_thread_t * p_intf;
122 for( i_timeout = 10 ; i_timeout-- ; )
124 if( ( p_intf = [NSApp getIntf] ) == NULL )
126 msleep( INTF_IDLE_SLEEP );
132 msg_Err( p_vout, "MacOS X intf has getIntf, but is NULL" );
133 free( p_vout->p_sys );
139 p_vout->p_sys->h_img_descr =
140 (ImageDescriptionHandle)NewHandleClear( sizeof(ImageDescription) );
141 p_vout->p_sys->p_matrix = (MatrixRecordPtr)malloc( sizeof(MatrixRecord) );
142 p_vout->p_sys->p_fullscreen_state = NULL;
144 p_vout->p_sys->b_mouse_pointer_visible = YES;
145 p_vout->p_sys->b_mouse_moved = YES;
146 p_vout->p_sys->i_time_mouse_last_moved = mdate();
148 if( value_drawable.i_int != 0 )
150 p_vout->p_sys->mask = NewRgn();
151 p_vout->p_sys->rect.left = 0 ;
152 p_vout->p_sys->rect.right = 0 ;
153 p_vout->p_sys->rect.top = 0 ;
154 p_vout->p_sys->rect.bottom = 0 ;
156 p_vout->p_sys->isplugin = 1 ;
160 p_vout->p_sys->mask = NULL;
161 p_vout->p_sys->isplugin = 0 ;
164 /* set window size */
165 p_vout->p_sys->s_rect.size.width = p_vout->i_window_width;
166 p_vout->p_sys->s_rect.size.height = p_vout->i_window_height;
168 if( ( err = EnterMovies() ) != noErr )
170 msg_Err( p_vout, "EnterMovies failed: %d", err );
171 free( p_vout->p_sys->p_matrix );
172 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
173 free( p_vout->p_sys );
177 /* Damn QT isn't thread safe. so keep a lock in the p_vlc object */
178 vlc_mutex_lock( &p_vout->p_vlc->quicktime_lock );
180 err = FindCodec( kYUV420CodecType, bestSpeedCodec,
181 nil, &p_vout->p_sys->img_dc );
183 vlc_mutex_unlock( &p_vout->p_vlc->quicktime_lock );
184 if( err == noErr && p_vout->p_sys->img_dc != 0 )
186 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
187 p_vout->p_sys->i_codec = kYUV420CodecType;
191 msg_Err( p_vout, "failed to find an appropriate codec" );
194 if( p_vout->p_sys->img_dc == 0 )
196 free( p_vout->p_sys->p_matrix );
197 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
198 free( p_vout->p_sys );
202 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
203 NSArray * o_screens = [NSScreen screens];
204 if( [o_screens count] > 0 && var_Type( p_vout, "video-device" ) == 0 )
207 vlc_value_t val, text;
210 int i_option = config_GetInt( p_vout, "macosx-vdev" );
212 var_Create( p_vout, "video-device", VLC_VAR_INTEGER |
214 text.psz_string = _("Video device");
215 var_Change( p_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL );
217 NSEnumerator * o_enumerator = [o_screens objectEnumerator];
219 while( (o_screen = [o_enumerator nextObject]) != NULL )
222 NSRect s_rect = [o_screen frame];
224 snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
225 "%s %d (%dx%d)", _("Screen"), i,
226 (int)s_rect.size.width, (int)s_rect.size.height );
228 text.psz_string = psz_temp;
230 var_Change( p_vout, "video-device",
231 VLC_VAR_ADDCHOICE, &val, &text );
233 if( ( i - 1 ) == i_option )
235 var_Set( p_vout, "video-device", val );
240 var_AddCallback( p_vout, "video-device", vout_VarCallback,
243 val.b_bool = VLC_TRUE;
244 var_Set( p_vout, "intf-change", val );
248 /* We don't need a window either in the mozilla plugin */
249 if( p_vout->p_sys->isplugin == 0 )
251 if( CoCreateWindow( p_vout ) )
253 msg_Err( p_vout, "unable to create window" );
254 free( p_vout->p_sys->p_matrix );
255 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
256 free( p_vout->p_sys );
261 p_vout->pf_init = vout_Init;
262 p_vout->pf_end = vout_End;
263 p_vout->pf_manage = vout_Manage;
264 p_vout->pf_render = NULL;
265 p_vout->pf_display = vout_Display;
270 /*****************************************************************************
271 * vout_Init: initialize video thread output method
272 *****************************************************************************/
273 static int vout_Init( vout_thread_t *p_vout )
279 I_OUTPUTPICTURES = 0;
281 /* Initialize the output structure; we already found a codec,
282 * and the corresponding chroma we will be using. Since we can
283 * arbitrary scale, stick to the coordinates and aspect. */
284 p_vout->output.i_width = p_vout->render.i_width;
285 p_vout->output.i_height = p_vout->render.i_height;
286 p_vout->output.i_aspect = p_vout->render.i_aspect;
288 var_Get( p_vout->p_vlc, "drawable", &val );
290 if( p_vout->p_sys->isplugin )
292 p_vout->p_sys->p_qdport = val.i_int;
295 SetPort( p_vout->p_sys->p_qdport );
296 QTScaleMatrix( p_vout );
298 if( QTCreateSequence( p_vout ) )
300 msg_Err( p_vout, "unable to create sequence" );
304 /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
305 while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
309 /* Find an empty picture slot */
310 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
312 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
314 p_pic = p_vout->p_picture + i_index;
319 /* Allocate the picture */
320 if( p_pic == NULL || QTNewPicture( p_vout, p_pic ) )
325 p_pic->i_status = DESTROYED_PICTURE;
326 p_pic->i_type = DIRECT_PICTURE;
328 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
336 /*****************************************************************************
337 * vout_End: terminate video thread output method
338 *****************************************************************************/
339 static void vout_End( vout_thread_t *p_vout )
343 QTDestroySequence( p_vout );
345 /* Free the direct buffers we allocated */
346 for( i_index = I_OUTPUTPICTURES; i_index; )
349 QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
353 /*****************************************************************************
354 * CloseVideo: destroy video thread output method
355 *****************************************************************************/
356 void E_(CloseVideo) ( vlc_object_t *p_this )
358 vout_thread_t * p_vout = (vout_thread_t *)p_this;
360 if ( p_vout->p_sys->isplugin == 0)
362 if( CoDestroyWindow( p_vout ) )
364 msg_Err( p_vout, "unable to destroy window" );
368 if ( p_vout->p_sys->p_fullscreen_state != NULL )
369 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
373 free( p_vout->p_sys->p_matrix );
374 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
376 free( p_vout->p_sys );
379 /*****************************************************************************
380 * vout_Manage: handle events
381 *****************************************************************************
382 * This function should be called regularly by video output thread. It manages
383 * console events. It returns a non null value on error.
384 *****************************************************************************/
385 static int vout_Manage( vout_thread_t *p_vout )
388 var_Get( p_vout->p_vlc, "drawableredraw", &val1 );
390 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
392 if( CoToggleFullscreen( p_vout ) )
397 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
400 if( (p_vout->i_changes & VOUT_SIZE_CHANGE) || (val1.i_int == 1))
405 var_Set( p_vout->p_vlc, "drawableredraw", val1 );
406 SetDSequenceMask( p_vout->p_sys->i_seq , p_vout->p_sys->mask );
407 } else if (p_vout->i_changes & VOUT_SIZE_CHANGE)
409 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
412 QTScaleMatrix( p_vout );
413 SetDSequenceMatrix( p_vout->p_sys->i_seq,
414 p_vout->p_sys->p_matrix );
417 /* hide/show mouse cursor
418 * this code looks unnecessarily complicated, but is necessary like this.
419 * it has to deal with multiple monitors and therefore checks a lot */
420 if( !p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
422 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 &&
423 p_vout->p_sys->b_mouse_pointer_visible )
425 VLCHideMouse( p_vout, YES );
427 else if ( !p_vout->p_sys->b_mouse_pointer_visible )
429 vlc_bool_t b_playing = NO;
430 input_thread_t * p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
433 if ( p_input != NULL )
436 var_Get( p_input, "state", &state );
437 b_playing = state.i_int != PAUSE_S;
438 vlc_object_release( p_input );
442 VLCHideMouse( p_vout, NO );
446 else if ( p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
448 if( !p_vout->p_sys->b_mouse_pointer_visible )
450 VLCHideMouse( p_vout, NO );
454 p_vout->p_sys->b_mouse_moved = NO;
461 /*****************************************************************************
462 * vout_Display: displays previously rendered output
463 *****************************************************************************
464 * This function sends the currently rendered image to the display.
465 *****************************************************************************/
466 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
473 if( p_vout->p_sys->isplugin )
477 /* In mozilla plugin, mozilla browser also draws things in
478 * the windows. So we have to update the port/Origin for each
479 * picture. FIXME : the vout should lock something ! */
480 GetPort( &p_vout->p_sys->p_qdportold );
481 GetPortBounds( p_vout->p_sys->p_qdportold, &oldrect );
484 LockPortBits( p_vout->p_sys->p_qdport );
486 SetPort( p_vout->p_sys->p_qdport );
487 SetOrigin( p_vout->p_sys->portx , p_vout->p_sys->porty );
488 ClipRect( &p_vout->p_sys->rect );
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, p_vout->p_sys->mask );
505 SetOrigin( oldrect.left , oldrect.top );
507 SetPort( p_vout->p_sys->p_qdportold );
509 UnlockPortBits( p_vout->p_sys->p_qdport );
514 if( ( err = DecompressSequenceFrameS(
515 p_vout->p_sys->i_seq,
516 p_pic->p_sys->p_info,
517 p_pic->p_sys->i_size,
518 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
520 msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
524 QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
529 /*****************************************************************************
530 * CoSendRequest: send request to interface thread
531 *****************************************************************************
532 * Returns 0 on success, 1 otherwise
533 *****************************************************************************/
534 static int CoSendRequest( vout_thread_t *p_vout, SEL sel )
538 VLCVout * o_vlv = [[VLCVout alloc] init];
540 if( ( i_ret = ExecuteOnMainThread( o_vlv, sel, (void *)p_vout ) ) )
542 msg_Err( p_vout, "SendRequest: no way to communicate with mt" );
550 /*****************************************************************************
551 * CoCreateWindow: create new window
552 *****************************************************************************
553 * Returns 0 on success, 1 otherwise
554 *****************************************************************************/
555 static int CoCreateWindow( vout_thread_t *p_vout )
557 if( CoSendRequest( p_vout, @selector(createWindow:) ) )
559 msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
566 /*****************************************************************************
567 * CoDestroyWindow: destroy window
568 *****************************************************************************
569 * Returns 0 on success, 1 otherwise
570 *****************************************************************************/
571 static int CoDestroyWindow( vout_thread_t *p_vout )
573 if( !p_vout->p_sys->b_mouse_pointer_visible )
575 VLCHideMouse( p_vout, NO );
578 if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
580 msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
587 /*****************************************************************************
588 * CoToggleFullscreen: toggle fullscreen
589 *****************************************************************************
590 * Returns 0 on success, 1 otherwise
591 *****************************************************************************/
592 static int CoToggleFullscreen( vout_thread_t *p_vout )
594 QTDestroySequence( p_vout );
596 if( CoDestroyWindow( p_vout ) )
598 msg_Err( p_vout, "unable to destroy window" );
602 p_vout->b_fullscreen = !p_vout->b_fullscreen;
604 config_PutInt( p_vout, "fullscreen", p_vout->b_fullscreen );
606 if( CoCreateWindow( p_vout ) )
608 msg_Err( p_vout, "unable to create window" );
612 if( QTCreateSequence( p_vout ) )
614 msg_Err( p_vout, "unable to create sequence" );
621 /*****************************************************************************
622 * VLCHideMouse: if b_hide then hide the cursor
623 *****************************************************************************/
624 static void VLCHideMouse ( vout_thread_t *p_vout, BOOL b_hide )
629 NSWindow *o_window = p_vout->p_sys->o_window;
630 NSView *o_contents = [o_window contentView];
632 s_rect = [o_contents bounds];
633 ml = [o_window convertScreenToBase:[NSEvent mouseLocation]];
634 ml = [o_contents convertPoint:ml fromView:nil];
635 b_inside = [o_contents mouse: ml inRect: s_rect];
637 if ( b_hide && b_inside )
639 /* only hide if mouse over VLCView */
641 p_vout->p_sys->b_mouse_pointer_visible = 0;
646 p_vout->p_sys->b_mouse_pointer_visible = 1;
648 p_vout->p_sys->b_mouse_moved = NO;
649 p_vout->p_sys->i_time_mouse_last_moved = mdate();
653 /*****************************************************************************
654 * QTScaleMatrix: scale matrix
655 *****************************************************************************/
656 static void QTScaleMatrix( vout_thread_t *p_vout )
659 unsigned int i_width, i_height;
660 Fixed factor_x, factor_y;
661 unsigned int i_offset_x = 0;
662 unsigned int i_offset_y = 0;
672 vlc_value_t valportx;
673 vlc_value_t valporty;
675 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
676 i_width = s_rect.right - s_rect.left;
677 i_height = s_rect.bottom - s_rect.top;
679 var_Get( p_vout->p_vlc, "drawable", &val );
680 var_Get( p_vout->p_vlc, "drawablet", &valt );
681 var_Get( p_vout->p_vlc, "drawablel", &vall );
682 var_Get( p_vout->p_vlc, "drawableb", &valb );
683 var_Get( p_vout->p_vlc, "drawabler", &valr );
684 var_Get( p_vout->p_vlc, "drawablex", &valx );
685 var_Get( p_vout->p_vlc, "drawabley", &valy );
686 var_Get( p_vout->p_vlc, "drawablew", &valw );
687 var_Get( p_vout->p_vlc, "drawableh", &valh );
688 var_Get( p_vout->p_vlc, "drawableportx", &valportx );
689 var_Get( p_vout->p_vlc, "drawableporty", &valporty );
691 if( p_vout->p_sys->isplugin )
693 p_vout->p_sys->portx = valportx.i_int;
694 p_vout->p_sys->porty = valporty.i_int;
695 p_vout->p_sys->p_qdport = val.i_int;
696 i_width = valw.i_int;
697 i_height = valh.i_int;
699 SetRectRgn( p_vout->p_sys->mask , vall.i_int - valx.i_int ,
700 valt.i_int - valy.i_int , valr.i_int - valx.i_int ,
701 valb.i_int - valy.i_int );
702 p_vout->p_sys->rect.top = 0;
703 p_vout->p_sys->rect.left = 0;
704 p_vout->p_sys->rect.bottom = valb.i_int - valt.i_int;
705 p_vout->p_sys->rect.right = valr.i_int - vall.i_int;
708 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
710 int i_adj_width = i_height * p_vout->output.i_aspect /
713 factor_x = FixDiv( Long2Fix( i_adj_width ),
714 Long2Fix( p_vout->output.i_width ) );
715 factor_y = FixDiv( Long2Fix( i_height ),
716 Long2Fix( p_vout->output.i_height ) );
718 i_offset_x = (i_width - i_adj_width) / 2 + i_offset_x;
722 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
723 p_vout->output.i_aspect;
725 factor_x = FixDiv( Long2Fix( i_width ),
726 Long2Fix( p_vout->output.i_width ) );
727 factor_y = FixDiv( Long2Fix( i_adj_height ),
728 Long2Fix( p_vout->output.i_height ) );
730 i_offset_y = (i_height - i_adj_height) / 2 + i_offset_y;
733 SetIdentityMatrix( p_vout->p_sys->p_matrix );
735 ScaleMatrix( p_vout->p_sys->p_matrix,
737 Long2Fix(0), Long2Fix(0) );
739 TranslateMatrix( p_vout->p_sys->p_matrix,
740 Long2Fix(i_offset_x),
741 Long2Fix(i_offset_y) );
745 /*****************************************************************************
746 * QTCreateSequence: create a new sequence
747 *****************************************************************************
748 * Returns 0 on success, 1 otherwise
749 *****************************************************************************/
750 static int QTCreateSequence( vout_thread_t *p_vout )
753 ImageDescriptionPtr p_descr;
755 HLock( (Handle)p_vout->p_sys->h_img_descr );
756 p_descr = *p_vout->p_sys->h_img_descr;
758 p_descr->idSize = sizeof(ImageDescription);
759 p_descr->cType = p_vout->p_sys->i_codec;
760 p_descr->version = 1;
761 p_descr->revisionLevel = 0;
762 p_descr->vendor = 'appl';
763 p_descr->width = p_vout->output.i_width;
764 p_descr->height = p_vout->output.i_height;
765 p_descr->hRes = Long2Fix(72);
766 p_descr->vRes = Long2Fix(72);
767 p_descr->spatialQuality = codecLosslessQuality;
768 p_descr->frameCount = 1;
769 p_descr->clutID = -1;
770 p_descr->dataSize = 0;
774 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
777 if( ( err = DecompressSequenceBeginS(
778 &p_vout->p_sys->i_seq,
779 p_vout->p_sys->h_img_descr,
781 p_vout->p_sys->p_qdport,
783 p_vout->p_sys->p_matrix,
784 0, p_vout->p_sys->mask,
785 codecFlagUseImageBuffer,
786 codecLosslessQuality,
787 p_vout->p_sys->img_dc ) ) )
789 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
797 /*****************************************************************************
798 * QTDestroySequence: destroy sequence
799 *****************************************************************************/
800 static void QTDestroySequence( vout_thread_t *p_vout )
802 CDSequenceEnd( p_vout->p_sys->i_seq );
805 /*****************************************************************************
806 * QTNewPicture: allocate a picture
807 *****************************************************************************
808 * Returns 0 on success, 1 otherwise
809 *****************************************************************************/
810 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
812 int i_width = p_vout->output.i_width;
813 int i_height = p_vout->output.i_height;
815 /* We know the chroma, allocate a buffer which will be used
816 * directly by the decoder */
817 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
819 if( p_pic->p_sys == NULL )
824 switch( p_vout->output.i_chroma )
826 case VLC_FOURCC('I','4','2','0'):
828 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
829 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
831 /* Allocate the memory buffer */
832 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
833 16, i_width * i_height * 3 / 2 );
836 p_pic->Y_PIXELS = p_pic->p_data;
837 p_pic->p[Y_PLANE].i_lines = i_height;
838 p_pic->p[Y_PLANE].i_pitch = i_width;
839 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
840 p_pic->p[Y_PLANE].i_visible_pitch = i_width;
843 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
844 p_pic->p[U_PLANE].i_lines = i_height / 2;
845 p_pic->p[U_PLANE].i_pitch = i_width / 2;
846 p_pic->p[U_PLANE].i_pixel_pitch = 1;
847 p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
850 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
851 p_pic->p[V_PLANE].i_lines = i_height / 2;
852 p_pic->p[V_PLANE].i_pitch = i_width / 2;
853 p_pic->p[V_PLANE].i_pixel_pitch = 1;
854 p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
856 /* We allocated 3 planes */
859 #define P p_pic->p_sys->pixmap_i420
860 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
861 - p_pic->p_sys->p_info;
862 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
863 - p_pic->p_sys->p_info;
864 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
865 - p_pic->p_sys->p_info;
867 P.componentInfoY.rowBytes = i_width;
868 P.componentInfoCb.rowBytes = i_width / 2;
869 P.componentInfoCr.rowBytes = i_width / 2;
875 /* Unknown chroma, tell the guy to get lost */
876 free( p_pic->p_sys );
877 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
878 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
886 /*****************************************************************************
887 * QTFreePicture: destroy a picture allocated with QTNewPicture
888 *****************************************************************************/
889 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
891 switch( p_vout->output.i_chroma )
893 case VLC_FOURCC('I','4','2','0'):
894 free( p_pic->p_data_orig );
898 free( p_pic->p_sys );
901 /*****************************************************************************
902 * VLCWindow implementation
903 *****************************************************************************/
904 @implementation VLCWindow
906 - (void)setVout:(vout_thread_t *)_p_vout
911 - (vout_thread_t *)getVout
916 - (void)scaleWindowWithFactor: (float)factor
919 int i_corrected_height, i_corrected_width;
921 NSPoint topleftscreen;
923 if ( !p_vout->b_fullscreen )
926 topleftbase.y = [self frame].size.height;
927 topleftscreen = [self convertBaseToScreen: topleftbase];
929 if( p_vout->output.i_height * p_vout->output.i_aspect >
930 p_vout->output.i_width * VOUT_ASPECT_FACTOR )
932 i_corrected_width = p_vout->output.i_height * p_vout->output.i_aspect /
934 newsize.width = (int) ( i_corrected_width * factor );
935 newsize.height = (int) ( p_vout->render.i_height * factor );
939 i_corrected_height = p_vout->output.i_width * VOUT_ASPECT_FACTOR /
940 p_vout->output.i_aspect;
941 newsize.width = (int) ( p_vout->render.i_width * factor );
942 newsize.height = (int) ( i_corrected_height * factor );
945 [self setContentSize: newsize];
947 [self setFrameTopLeftPoint: topleftscreen];
948 p_vout->i_changes |= VOUT_SIZE_CHANGE;
952 - (void)toggleFloatOnTop
954 if( config_GetInt( p_vout, "macosx-float" ) )
956 config_PutInt( p_vout, "macosx-float", 0 );
957 [p_vout->p_sys->o_window setLevel: NSNormalWindowLevel];
961 config_PutInt( p_vout, "macosx-float", 1 );
962 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
966 - (void)toggleFullscreen
968 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
973 return( p_vout->b_fullscreen );
976 - (BOOL)canBecomeKeyWindow
981 - (void)keyDown:(NSEvent *)o_event
983 playlist_t * p_playlist;
987 if( [[o_event characters] length] )
989 key = [[o_event characters] characterAtIndex: 0];
995 [self toggleFullscreen];
998 case (unichar)0x1b: /* escape */
999 if( [self isFullscreen] )
1001 [self toggleFullscreen];
1006 p_vout->p_vlc->b_die = VLC_TRUE;
1010 p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1012 if ( p_playlist != NULL )
1014 playlist_Pause( p_playlist );
1015 vlc_object_release( p_playlist);
1019 case (unichar)0xf700: /* arrow up */
1020 val.psz_string = "UP";
1021 var_Set( p_vout, "key-pressed", val );
1024 case (unichar)0xf701: /* arrow down */
1025 val.psz_string = "DOWN";
1026 var_Set( p_vout, "key-pressed", val );
1029 case (unichar)0xf702: /* arrow left */
1030 val.psz_string = "LEFT";
1031 var_Set( p_vout, "key-pressed", val );
1034 case (unichar)0xf703: /* arrow right */
1035 val.psz_string = "RIGHT";
1036 var_Set( p_vout, "key-pressed", val );
1039 case (unichar)0xd: /* return */
1040 case (unichar)0x3: /* enter */
1041 val.psz_string = "ENTER";
1042 var_Set( p_vout, "key-pressed", val );
1047 [super keyDown: o_event];
1054 NSMutableString * o_title;
1055 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1058 if( p_playlist == NULL )
1063 vlc_mutex_lock( &p_playlist->object_lock );
1064 o_title = [NSMutableString stringWithUTF8String:
1065 p_playlist->pp_items[p_playlist->i_index]->psz_name];
1066 vlc_mutex_unlock( &p_playlist->object_lock );
1068 vlc_object_release( p_playlist );
1070 if( o_title != nil )
1072 NSRange prefix_range = [o_title rangeOfString: @"file:"];
1073 if( prefix_range.location != NSNotFound )
1075 [o_title deleteCharactersInRange: prefix_range];
1078 [self setTitleWithRepresentedFilename: o_title];
1083 [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
1087 /* This is actually the same as VLCControls::stop. */
1088 - (BOOL)windowShouldClose:(id)sender
1090 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1092 if( p_playlist == NULL )
1097 playlist_Stop( p_playlist );
1098 vlc_object_release( p_playlist );
1100 /* The window will be closed by the intf later. */
1106 /*****************************************************************************
1107 * VLCView implementation
1108 *****************************************************************************/
1109 @implementation VLCView
1111 - (void)drawRect:(NSRect)rect
1113 vout_thread_t * p_vout;
1114 id o_window = [self window];
1115 p_vout = (vout_thread_t *)[o_window getVout];
1117 [[NSColor blackColor] set];
1119 [super drawRect: rect];
1121 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1124 - (BOOL)acceptsFirstResponder
1129 - (BOOL)becomeFirstResponder
1131 vout_thread_t * p_vout;
1132 id o_window = [self window];
1133 p_vout = (vout_thread_t *)[o_window getVout];
1135 [o_window setAcceptsMouseMovedEvents: YES];
1139 - (BOOL)resignFirstResponder
1141 vout_thread_t * p_vout;
1142 id o_window = [self window];
1143 p_vout = (vout_thread_t *)[o_window getVout];
1145 [o_window setAcceptsMouseMovedEvents: NO];
1146 VLCHideMouse( p_vout, NO );
1150 - (void)mouseDown:(NSEvent *)o_event
1152 vout_thread_t * p_vout;
1153 id o_window = [self window];
1154 p_vout = (vout_thread_t *)[o_window getVout];
1157 switch( [o_event type] )
1159 case NSLeftMouseDown:
1161 var_Get( p_vout, "mouse-button-down", &val );
1163 var_Set( p_vout, "mouse-button-down", val );
1168 [super mouseDown: o_event];
1173 - (void)otherMouseDown:(NSEvent *)o_event
1175 /* This is not the the wheel button. you need to poll the
1176 * mouseWheel event for that. other is a third, forth or fifth button */
1177 vout_thread_t * p_vout;
1178 id o_window = [self window];
1179 p_vout = (vout_thread_t *)[o_window getVout];
1182 switch( [o_event type] )
1184 case NSOtherMouseDown:
1186 var_Get( p_vout, "mouse-button-down", &val );
1188 var_Set( p_vout, "mouse-button-down", val );
1193 [super mouseDown: o_event];
1198 - (void)rightMouseDown:(NSEvent *)o_event
1200 vout_thread_t * p_vout;
1201 id o_window = [self window];
1202 p_vout = (vout_thread_t *)[o_window getVout];
1205 switch( [o_event type] )
1207 case NSRightMouseDown:
1209 var_Get( p_vout, "mouse-button-down", &val );
1211 var_Set( p_vout, "mouse-button-down", val );
1216 [super mouseDown: o_event];
1221 - (void)mouseUp:(NSEvent *)o_event
1223 vout_thread_t * p_vout;
1224 id o_window = [self window];
1225 p_vout = (vout_thread_t *)[o_window getVout];
1228 switch( [o_event type] )
1233 b_val.b_bool = VLC_TRUE;
1234 var_Set( p_vout, "mouse-clicked", b_val );
1236 var_Get( p_vout, "mouse-button-down", &val );
1238 var_Set( p_vout, "mouse-button-down", val );
1243 [super mouseUp: o_event];
1248 - (void)otherMouseUp:(NSEvent *)o_event
1250 vout_thread_t * p_vout;
1251 id o_window = [self window];
1252 p_vout = (vout_thread_t *)[o_window getVout];
1255 switch( [o_event type] )
1257 case NSOtherMouseUp:
1259 var_Get( p_vout, "mouse-button-down", &val );
1261 var_Set( p_vout, "mouse-button-down", val );
1266 [super mouseUp: o_event];
1271 - (void)rightMouseUp:(NSEvent *)o_event
1273 vout_thread_t * p_vout;
1274 id o_window = [self window];
1275 p_vout = (vout_thread_t *)[o_window getVout];
1278 switch( [o_event type] )
1280 case NSRightMouseUp:
1282 var_Get( p_vout, "mouse-button-down", &val );
1284 var_Set( p_vout, "mouse-button-down", val );
1289 [super mouseUp: o_event];
1294 - (void)mouseDragged:(NSEvent *)o_event
1296 [self mouseMoved:o_event];
1299 - (void)otherMouseDragged:(NSEvent *)o_event
1301 [self mouseMoved:o_event];
1304 - (void)rightMouseDragged:(NSEvent *)o_event
1306 [self mouseMoved:o_event];
1309 - (void)mouseMoved:(NSEvent *)o_event
1315 vout_thread_t * p_vout;
1316 id o_window = [self window];
1317 p_vout = (vout_thread_t *)[o_window getVout];
1319 s_rect = [self bounds];
1320 ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1321 b_inside = [self mouse: ml inRect: s_rect];
1326 int i_width, i_height, i_x, i_y;
1328 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1329 (unsigned int)s_rect.size.height,
1330 &i_x, &i_y, &i_width, &i_height );
1332 val.i_int = ( ((int)ml.x) - i_x ) *
1333 p_vout->render.i_width / i_width;
1334 var_Set( p_vout, "mouse-x", val );
1336 val.i_int = ( ((int)ml.y) - i_y ) *
1337 p_vout->render.i_height / i_height;
1338 var_Set( p_vout, "mouse-y", val );
1340 val.b_bool = VLC_TRUE;
1341 var_Set( p_vout, "mouse-moved", val );
1342 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1343 p_vout->p_sys->b_mouse_moved = YES;
1345 else if ( !b_inside && !p_vout->p_sys->b_mouse_pointer_visible )
1347 /* people with multiple monitors need their mouse,
1348 * even if VLCView in fullscreen. */
1349 VLCHideMouse( p_vout, NO );
1352 [super mouseMoved: o_event];
1357 /*****************************************************************************
1358 * VLCVout implementation
1359 *****************************************************************************/
1360 @implementation VLCVout
1362 - (void)createWindow:(NSValue *)o_value
1366 NSScreen * o_screen;
1367 vout_thread_t * p_vout;
1368 vlc_bool_t b_main_screen;
1370 p_vout = (vout_thread_t *)[o_value pointerValue];
1372 p_vout->p_sys->o_window = [VLCWindow alloc];
1373 [p_vout->p_sys->o_window setVout: p_vout];
1374 [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1376 if( var_Get( p_vout, "video-device", &val ) < 0 )
1378 o_screen = [NSScreen mainScreen];
1383 NSArray *o_screens = [NSScreen screens];
1384 unsigned int i_index = val.i_int;
1386 if( [o_screens count] < i_index )
1388 o_screen = [NSScreen mainScreen];
1394 o_screen = [o_screens objectAtIndex: i_index];
1395 config_PutInt( p_vout, "macosx-vdev", i_index );
1396 b_main_screen = (i_index == 0);
1400 if( p_vout->b_fullscreen )
1402 NSRect screen_rect = [o_screen frame];
1403 screen_rect.origin.x = screen_rect.origin.y = 0;
1405 if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1406 BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1407 NULL, NULL, fullScreenAllowEvents );
1409 [p_vout->p_sys->o_window
1410 initWithContentRect: screen_rect
1411 styleMask: NSBorderlessWindowMask
1412 backing: NSBackingStoreBuffered
1413 defer: NO screen: o_screen];
1415 [p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1416 p_vout->p_sys->b_mouse_moved = YES;
1417 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1421 unsigned int i_stylemask = NSTitledWindowMask |
1422 NSMiniaturizableWindowMask |
1423 NSClosableWindowMask |
1424 NSResizableWindowMask;
1426 if ( p_vout->p_sys->p_fullscreen_state != NULL )
1427 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1428 p_vout->p_sys->p_fullscreen_state = NULL;
1430 [p_vout->p_sys->o_window
1431 initWithContentRect: p_vout->p_sys->s_rect
1432 styleMask: i_stylemask
1433 backing: NSBackingStoreBuffered
1434 defer: NO screen: o_screen];
1436 [p_vout->p_sys->o_window setAlphaValue: config_GetFloat( p_vout, "macosx-opaqueness" )];
1438 if( config_GetInt( p_vout, "macosx-float" ) )
1440 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
1443 if( !p_vout->p_sys->b_pos_saved )
1445 [p_vout->p_sys->o_window center];
1449 o_view = [[VLCView alloc] init];
1451 /* FIXME: [o_view setMenu:] */
1452 [p_vout->p_sys->o_window setContentView: o_view];
1453 [o_view autorelease];
1456 p_vout->p_sys->p_qdport = [o_view qdPort];
1458 [o_view unlockFocus];
1460 [p_vout->p_sys->o_window updateTitle];
1461 [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1464 - (void)destroyWindow:(NSValue *)o_value
1466 vout_thread_t * p_vout;
1468 p_vout = (vout_thread_t *)[o_value pointerValue];
1470 if( !p_vout->b_fullscreen )
1474 s_rect = [[p_vout->p_sys->o_window contentView] frame];
1475 p_vout->p_sys->s_rect.size = s_rect.size;
1477 s_rect = [p_vout->p_sys->o_window frame];
1478 p_vout->p_sys->s_rect.origin = s_rect.origin;
1480 p_vout->p_sys->b_pos_saved = YES;
1483 p_vout->p_sys->p_qdport = nil;
1484 [p_vout->p_sys->o_window close];
1485 p_vout->p_sys->o_window = nil;