2 /*****************************************************************************
3 * vout.m: MacOS X video output plugin
4 *****************************************************************************
5 * Copyright (C) 2001-2003 VideoLAN
6 * $Id: vout.m,v 1.54 2003/08/20 16:22:27 garf 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 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
433 if ( p_playlist != nil )
435 vlc_mutex_lock( &p_playlist->object_lock );
436 if( p_playlist->p_input != NULL )
438 vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
439 b_playing = p_playlist->p_input->stream.control.i_status != PAUSE_S;
440 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
442 vlc_mutex_unlock( &p_playlist->object_lock );
443 vlc_object_release( p_playlist );
447 VLCHideMouse( p_vout, NO );
451 else if ( p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
453 if( !p_vout->p_sys->b_mouse_pointer_visible )
455 VLCHideMouse( p_vout, NO );
459 p_vout->p_sys->b_mouse_moved = NO;
466 /*****************************************************************************
467 * vout_Display: displays previously rendered output
468 *****************************************************************************
469 * This function sends the currently rendered image to the display.
470 *****************************************************************************/
471 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
478 if( p_vout->p_sys->isplugin )
482 /* In mozilla plugin, mozilla browser also draws things in
483 * the windows. So we have to update the port/Origin for each
484 * picture. FIXME : the vout should lock something ! */
485 GetPort( &p_vout->p_sys->p_qdportold );
486 GetPortBounds( p_vout->p_sys->p_qdportold, &oldrect );
489 SetPort( p_vout->p_sys->p_qdport );
490 SetOrigin( p_vout->p_sys->portx , p_vout->p_sys->porty );
491 ClipRect( &p_vout->p_sys->rect );
493 if( ( err = DecompressSequenceFrameS(
494 p_vout->p_sys->i_seq,
495 p_pic->p_sys->p_info,
496 p_pic->p_sys->i_size,
497 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
499 msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
503 QDFlushPortBuffer( p_vout->p_sys->p_qdport, p_vout->p_sys->mask );
506 SetOrigin( oldrect.left , oldrect.top );
508 SetPort( p_vout->p_sys->p_qdportold );
512 if( ( err = DecompressSequenceFrameS(
513 p_vout->p_sys->i_seq,
514 p_pic->p_sys->p_info,
515 p_pic->p_sys->i_size,
516 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
518 msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
522 QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
527 /*****************************************************************************
528 * CoSendRequest: send request to interface thread
529 *****************************************************************************
530 * Returns 0 on success, 1 otherwise
531 *****************************************************************************/
532 static int CoSendRequest( vout_thread_t *p_vout, SEL sel )
536 VLCVout * o_vlv = [[VLCVout alloc] init];
538 if( ( i_ret = ExecuteOnMainThread( o_vlv, sel, (void *)p_vout ) ) )
540 msg_Err( p_vout, "SendRequest: no way to communicate with mt" );
548 /*****************************************************************************
549 * CoCreateWindow: create new window
550 *****************************************************************************
551 * Returns 0 on success, 1 otherwise
552 *****************************************************************************/
553 static int CoCreateWindow( vout_thread_t *p_vout )
555 if( CoSendRequest( p_vout, @selector(createWindow:) ) )
557 msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
564 /*****************************************************************************
565 * CoDestroyWindow: destroy window
566 *****************************************************************************
567 * Returns 0 on success, 1 otherwise
568 *****************************************************************************/
569 static int CoDestroyWindow( vout_thread_t *p_vout )
571 if( !p_vout->p_sys->b_mouse_pointer_visible )
573 VLCHideMouse( p_vout, NO );
576 if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
578 msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
585 /*****************************************************************************
586 * CoToggleFullscreen: toggle fullscreen
587 *****************************************************************************
588 * Returns 0 on success, 1 otherwise
589 *****************************************************************************/
590 static int CoToggleFullscreen( vout_thread_t *p_vout )
592 QTDestroySequence( p_vout );
594 if( CoDestroyWindow( p_vout ) )
596 msg_Err( p_vout, "unable to destroy window" );
600 p_vout->b_fullscreen = !p_vout->b_fullscreen;
602 config_PutInt( p_vout, "fullscreen", p_vout->b_fullscreen );
604 if( CoCreateWindow( p_vout ) )
606 msg_Err( p_vout, "unable to create window" );
610 if( QTCreateSequence( p_vout ) )
612 msg_Err( p_vout, "unable to create sequence" );
619 /*****************************************************************************
620 * VLCHideMouse: if b_hide then hide the cursor
621 *****************************************************************************/
622 static void VLCHideMouse ( vout_thread_t *p_vout, BOOL b_hide )
627 NSWindow *o_window = p_vout->p_sys->o_window;
628 NSView *o_contents = [o_window contentView];
630 s_rect = [o_contents bounds];
631 ml = [o_window convertScreenToBase:[NSEvent mouseLocation]];
632 ml = [o_contents convertPoint:ml fromView:nil];
633 b_inside = [o_contents mouse: ml inRect: s_rect];
635 if ( b_hide && b_inside )
637 /* only hide if mouse over VLCView */
639 p_vout->p_sys->b_mouse_pointer_visible = 0;
644 p_vout->p_sys->b_mouse_pointer_visible = 1;
646 p_vout->p_sys->b_mouse_moved = NO;
647 p_vout->p_sys->i_time_mouse_last_moved = mdate();
651 /*****************************************************************************
652 * QTScaleMatrix: scale matrix
653 *****************************************************************************/
654 static void QTScaleMatrix( vout_thread_t *p_vout )
657 unsigned int i_width, i_height;
658 Fixed factor_x, factor_y;
659 unsigned int i_offset_x = 0;
660 unsigned int i_offset_y = 0;
670 vlc_value_t valportx;
671 vlc_value_t valporty;
673 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
674 i_width = s_rect.right - s_rect.left;
675 i_height = s_rect.bottom - s_rect.top;
677 var_Get( p_vout->p_vlc, "drawable", &val );
678 var_Get( p_vout->p_vlc, "drawablet", &valt );
679 var_Get( p_vout->p_vlc, "drawablel", &vall );
680 var_Get( p_vout->p_vlc, "drawableb", &valb );
681 var_Get( p_vout->p_vlc, "drawabler", &valr );
682 var_Get( p_vout->p_vlc, "drawablex", &valx );
683 var_Get( p_vout->p_vlc, "drawabley", &valy );
684 var_Get( p_vout->p_vlc, "drawablew", &valw );
685 var_Get( p_vout->p_vlc, "drawableh", &valh );
686 var_Get( p_vout->p_vlc, "drawableportx", &valportx );
687 var_Get( p_vout->p_vlc, "drawableporty", &valporty );
689 if( p_vout->p_sys->isplugin )
691 p_vout->p_sys->portx = valportx.i_int;
692 p_vout->p_sys->porty = valporty.i_int;
693 p_vout->p_sys->p_qdport = val.i_int;
694 i_width = valw.i_int;
695 i_height = valh.i_int;
697 SetRectRgn( p_vout->p_sys->mask , vall.i_int - valx.i_int ,
698 valt.i_int - valy.i_int , valr.i_int - valx.i_int ,
699 valb.i_int - valy.i_int );
700 p_vout->p_sys->rect.top = 0;
701 p_vout->p_sys->rect.left = 0;
702 p_vout->p_sys->rect.bottom = valb.i_int - valt.i_int;
703 p_vout->p_sys->rect.right = valr.i_int - vall.i_int;
706 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
708 int i_adj_width = i_height * p_vout->output.i_aspect /
711 factor_x = FixDiv( Long2Fix( i_adj_width ),
712 Long2Fix( p_vout->output.i_width ) );
713 factor_y = FixDiv( Long2Fix( i_height ),
714 Long2Fix( p_vout->output.i_height ) );
716 i_offset_x = (i_width - i_adj_width) / 2 + i_offset_x;
720 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
721 p_vout->output.i_aspect;
723 factor_x = FixDiv( Long2Fix( i_width ),
724 Long2Fix( p_vout->output.i_width ) );
725 factor_y = FixDiv( Long2Fix( i_adj_height ),
726 Long2Fix( p_vout->output.i_height ) );
728 i_offset_y = (i_height - i_adj_height) / 2 + i_offset_y;
731 SetIdentityMatrix( p_vout->p_sys->p_matrix );
733 ScaleMatrix( p_vout->p_sys->p_matrix,
735 Long2Fix(0), Long2Fix(0) );
737 TranslateMatrix( p_vout->p_sys->p_matrix,
738 Long2Fix(i_offset_x),
739 Long2Fix(i_offset_y) );
743 /*****************************************************************************
744 * QTCreateSequence: create a new sequence
745 *****************************************************************************
746 * Returns 0 on success, 1 otherwise
747 *****************************************************************************/
748 static int QTCreateSequence( vout_thread_t *p_vout )
751 ImageDescriptionPtr p_descr;
753 HLock( (Handle)p_vout->p_sys->h_img_descr );
754 p_descr = *p_vout->p_sys->h_img_descr;
756 p_descr->idSize = sizeof(ImageDescription);
757 p_descr->cType = p_vout->p_sys->i_codec;
758 p_descr->version = 1;
759 p_descr->revisionLevel = 0;
760 p_descr->vendor = 'appl';
761 p_descr->width = p_vout->output.i_width;
762 p_descr->height = p_vout->output.i_height;
763 p_descr->hRes = Long2Fix(72);
764 p_descr->vRes = Long2Fix(72);
765 p_descr->spatialQuality = codecLosslessQuality;
766 p_descr->frameCount = 1;
767 p_descr->clutID = -1;
768 p_descr->dataSize = 0;
772 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
775 if( ( err = DecompressSequenceBeginS(
776 &p_vout->p_sys->i_seq,
777 p_vout->p_sys->h_img_descr,
779 p_vout->p_sys->p_qdport,
781 p_vout->p_sys->p_matrix,
782 0, p_vout->p_sys->mask,
783 codecFlagUseImageBuffer,
784 codecLosslessQuality,
785 p_vout->p_sys->img_dc ) ) )
787 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
795 /*****************************************************************************
796 * QTDestroySequence: destroy sequence
797 *****************************************************************************/
798 static void QTDestroySequence( vout_thread_t *p_vout )
800 CDSequenceEnd( p_vout->p_sys->i_seq );
803 /*****************************************************************************
804 * QTNewPicture: allocate a picture
805 *****************************************************************************
806 * Returns 0 on success, 1 otherwise
807 *****************************************************************************/
808 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
810 int i_width = p_vout->output.i_width;
811 int i_height = p_vout->output.i_height;
813 /* We know the chroma, allocate a buffer which will be used
814 * directly by the decoder */
815 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
817 if( p_pic->p_sys == NULL )
822 switch( p_vout->output.i_chroma )
824 case VLC_FOURCC('I','4','2','0'):
826 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
827 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
829 /* Allocate the memory buffer */
830 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
831 16, i_width * i_height * 3 / 2 );
834 p_pic->Y_PIXELS = p_pic->p_data;
835 p_pic->p[Y_PLANE].i_lines = i_height;
836 p_pic->p[Y_PLANE].i_pitch = i_width;
837 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
838 p_pic->p[Y_PLANE].i_visible_pitch = i_width;
841 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
842 p_pic->p[U_PLANE].i_lines = i_height / 2;
843 p_pic->p[U_PLANE].i_pitch = i_width / 2;
844 p_pic->p[U_PLANE].i_pixel_pitch = 1;
845 p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
848 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
849 p_pic->p[V_PLANE].i_lines = i_height / 2;
850 p_pic->p[V_PLANE].i_pitch = i_width / 2;
851 p_pic->p[V_PLANE].i_pixel_pitch = 1;
852 p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
854 /* We allocated 3 planes */
857 #define P p_pic->p_sys->pixmap_i420
858 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
859 - p_pic->p_sys->p_info;
860 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
861 - p_pic->p_sys->p_info;
862 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
863 - p_pic->p_sys->p_info;
865 P.componentInfoY.rowBytes = i_width;
866 P.componentInfoCb.rowBytes = i_width / 2;
867 P.componentInfoCr.rowBytes = i_width / 2;
873 /* Unknown chroma, tell the guy to get lost */
874 free( p_pic->p_sys );
875 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
876 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
884 /*****************************************************************************
885 * QTFreePicture: destroy a picture allocated with QTNewPicture
886 *****************************************************************************/
887 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
889 switch( p_vout->output.i_chroma )
891 case VLC_FOURCC('I','4','2','0'):
892 free( p_pic->p_data_orig );
896 free( p_pic->p_sys );
899 /*****************************************************************************
900 * VLCWindow implementation
901 *****************************************************************************/
902 @implementation VLCWindow
904 - (void)setVout:(vout_thread_t *)_p_vout
909 - (vout_thread_t *)getVout
914 - (void)scaleWindowWithFactor: (float)factor
917 int i_corrected_height, i_corrected_width;
919 NSPoint topleftscreen;
921 if ( !p_vout->b_fullscreen )
924 topleftbase.y = [self frame].size.height;
925 topleftscreen = [self convertBaseToScreen: topleftbase];
927 if( p_vout->output.i_height * p_vout->output.i_aspect >
928 p_vout->output.i_width * VOUT_ASPECT_FACTOR )
930 i_corrected_width = p_vout->output.i_height * p_vout->output.i_aspect /
932 newsize.width = (int) ( i_corrected_width * factor );
933 newsize.height = (int) ( p_vout->render.i_height * factor );
937 i_corrected_height = p_vout->output.i_width * VOUT_ASPECT_FACTOR /
938 p_vout->output.i_aspect;
939 newsize.width = (int) ( p_vout->render.i_width * factor );
940 newsize.height = (int) ( i_corrected_height * factor );
943 [self setContentSize: newsize];
945 [self setFrameTopLeftPoint: topleftscreen];
946 p_vout->i_changes |= VOUT_SIZE_CHANGE;
950 - (void)toggleFloatOnTop
952 if( config_GetInt( p_vout, "macosx-float" ) )
954 config_PutInt( p_vout, "macosx-float", 0 );
955 [p_vout->p_sys->o_window setLevel: NSNormalWindowLevel];
959 config_PutInt( p_vout, "macosx-float", 1 );
960 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
964 - (void)toggleFullscreen
966 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
971 return( p_vout->b_fullscreen );
974 - (BOOL)canBecomeKeyWindow
979 - (void)keyDown:(NSEvent *)o_event
984 if( [[o_event characters] length] )
986 key = [[o_event characters] characterAtIndex: 0];
992 [self toggleFullscreen];
995 case (unichar)0x1b: /* escape */
996 if( [self isFullscreen] )
998 [self toggleFullscreen];
1003 p_vout->p_vlc->b_die = VLC_TRUE;
1007 input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
1010 case (unichar)0xf700: /* arrow up */
1011 val.psz_string = "UP";
1012 var_Set( p_vout, "key-pressed", val );
1015 case (unichar)0xf701: /* arrow down */
1016 val.psz_string = "DOWN";
1017 var_Set( p_vout, "key-pressed", val );
1020 case (unichar)0xf702: /* arrow left */
1021 val.psz_string = "LEFT";
1022 var_Set( p_vout, "key-pressed", val );
1025 case (unichar)0xf703: /* arrow right */
1026 val.psz_string = "RIGHT";
1027 var_Set( p_vout, "key-pressed", val );
1030 case (unichar)0xd: /* return */
1031 case (unichar)0x3: /* enter */
1032 val.psz_string = "ENTER";
1033 var_Set( p_vout, "key-pressed", val );
1038 [super keyDown: o_event];
1045 NSMutableString * o_title;
1046 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1049 if( p_playlist == NULL )
1054 vlc_mutex_lock( &p_playlist->object_lock );
1055 o_title = [NSMutableString stringWithUTF8String:
1056 p_playlist->pp_items[p_playlist->i_index]->psz_name];
1057 vlc_mutex_unlock( &p_playlist->object_lock );
1059 vlc_object_release( p_playlist );
1061 if( o_title != nil )
1063 NSRange prefix_range = [o_title rangeOfString: @"file:"];
1064 if( prefix_range.location != NSNotFound )
1066 [o_title deleteCharactersInRange: prefix_range];
1069 [self setTitleWithRepresentedFilename: o_title];
1074 [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
1078 /* This is actually the same as VLCControls::stop. */
1079 - (BOOL)windowShouldClose:(id)sender
1081 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1083 if( p_playlist == NULL )
1088 playlist_Stop( p_playlist );
1089 vlc_object_release( p_playlist );
1091 /* The window will be closed by the intf later. */
1097 /*****************************************************************************
1098 * VLCView implementation
1099 *****************************************************************************/
1100 @implementation VLCView
1102 - (void)drawRect:(NSRect)rect
1104 vout_thread_t * p_vout;
1105 id o_window = [self window];
1106 p_vout = (vout_thread_t *)[o_window getVout];
1108 [[NSColor blackColor] set];
1110 [super drawRect: rect];
1112 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1115 - (BOOL)acceptsFirstResponder
1120 - (BOOL)becomeFirstResponder
1122 vout_thread_t * p_vout;
1123 id o_window = [self window];
1124 p_vout = (vout_thread_t *)[o_window getVout];
1126 [o_window setAcceptsMouseMovedEvents: YES];
1130 - (BOOL)resignFirstResponder
1132 vout_thread_t * p_vout;
1133 id o_window = [self window];
1134 p_vout = (vout_thread_t *)[o_window getVout];
1136 [o_window setAcceptsMouseMovedEvents: NO];
1137 VLCHideMouse( p_vout, NO );
1141 - (void)mouseDown:(NSEvent *)o_event
1143 vout_thread_t * p_vout;
1144 id o_window = [self window];
1145 p_vout = (vout_thread_t *)[o_window getVout];
1148 switch( [o_event type] )
1150 case NSLeftMouseDown:
1152 var_Get( p_vout, "mouse-button-down", &val );
1154 var_Set( p_vout, "mouse-button-down", val );
1159 [super mouseDown: o_event];
1164 - (void)otherMouseDown:(NSEvent *)o_event
1166 /* This is not the the wheel button. you need to poll the
1167 * mouseWheel event for that. other is a third, forth or fifth button */
1168 vout_thread_t * p_vout;
1169 id o_window = [self window];
1170 p_vout = (vout_thread_t *)[o_window getVout];
1173 switch( [o_event type] )
1175 case NSOtherMouseDown:
1177 var_Get( p_vout, "mouse-button-down", &val );
1179 var_Set( p_vout, "mouse-button-down", val );
1184 [super mouseDown: o_event];
1189 - (void)rightMouseDown:(NSEvent *)o_event
1191 vout_thread_t * p_vout;
1192 id o_window = [self window];
1193 p_vout = (vout_thread_t *)[o_window getVout];
1196 switch( [o_event type] )
1198 case NSRightMouseDown:
1200 var_Get( p_vout, "mouse-button-down", &val );
1202 var_Set( p_vout, "mouse-button-down", val );
1207 [super mouseDown: o_event];
1212 - (void)mouseUp:(NSEvent *)o_event
1214 vout_thread_t * p_vout;
1215 id o_window = [self window];
1216 p_vout = (vout_thread_t *)[o_window getVout];
1219 switch( [o_event type] )
1224 b_val.b_bool = VLC_TRUE;
1225 var_Set( p_vout, "mouse-clicked", b_val );
1227 var_Get( p_vout, "mouse-button-down", &val );
1229 var_Set( p_vout, "mouse-button-down", val );
1234 [super mouseUp: o_event];
1239 - (void)otherMouseUp:(NSEvent *)o_event
1241 vout_thread_t * p_vout;
1242 id o_window = [self window];
1243 p_vout = (vout_thread_t *)[o_window getVout];
1246 switch( [o_event type] )
1248 case NSOtherMouseUp:
1250 var_Get( p_vout, "mouse-button-down", &val );
1252 var_Set( p_vout, "mouse-button-down", val );
1257 [super mouseUp: o_event];
1262 - (void)rightMouseUp:(NSEvent *)o_event
1264 vout_thread_t * p_vout;
1265 id o_window = [self window];
1266 p_vout = (vout_thread_t *)[o_window getVout];
1269 switch( [o_event type] )
1271 case NSRightMouseUp:
1273 var_Get( p_vout, "mouse-button-down", &val );
1275 var_Set( p_vout, "mouse-button-down", val );
1280 [super mouseUp: o_event];
1285 - (void)mouseDragged:(NSEvent *)o_event
1287 [self mouseMoved:o_event];
1290 - (void)otherMouseDragged:(NSEvent *)o_event
1292 [self mouseMoved:o_event];
1295 - (void)rightMouseDragged:(NSEvent *)o_event
1297 [self mouseMoved:o_event];
1300 - (void)mouseMoved:(NSEvent *)o_event
1306 vout_thread_t * p_vout;
1307 id o_window = [self window];
1308 p_vout = (vout_thread_t *)[o_window getVout];
1310 s_rect = [self bounds];
1311 ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1312 b_inside = [self mouse: ml inRect: s_rect];
1317 int i_width, i_height, i_x, i_y;
1319 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1320 (unsigned int)s_rect.size.height,
1321 &i_x, &i_y, &i_width, &i_height );
1323 val.i_int = ( ((int)ml.x) - i_x ) *
1324 p_vout->render.i_width / i_width;
1325 var_Set( p_vout, "mouse-x", val );
1327 val.i_int = ( ((int)ml.y) - i_y ) *
1328 p_vout->render.i_height / i_height;
1329 var_Set( p_vout, "mouse-y", val );
1331 val.b_bool = VLC_TRUE;
1332 var_Set( p_vout, "mouse-moved", val );
1333 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1334 p_vout->p_sys->b_mouse_moved = YES;
1336 else if ( !b_inside && !p_vout->p_sys->b_mouse_pointer_visible )
1338 /* people with multiple monitors need their mouse,
1339 * even if VLCView in fullscreen. */
1340 VLCHideMouse( p_vout, NO );
1343 [super mouseMoved: o_event];
1348 /*****************************************************************************
1349 * VLCVout implementation
1350 *****************************************************************************/
1351 @implementation VLCVout
1353 - (void)createWindow:(NSValue *)o_value
1357 NSScreen * o_screen;
1358 vout_thread_t * p_vout;
1359 vlc_bool_t b_main_screen;
1361 p_vout = (vout_thread_t *)[o_value pointerValue];
1363 p_vout->p_sys->o_window = [VLCWindow alloc];
1364 [p_vout->p_sys->o_window setVout: p_vout];
1365 [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1367 if( var_Get( p_vout, "video-device", &val ) < 0 )
1369 o_screen = [NSScreen mainScreen];
1374 NSArray *o_screens = [NSScreen screens];
1375 unsigned int i_index = val.i_int;
1377 if( [o_screens count] < i_index )
1379 o_screen = [NSScreen mainScreen];
1385 o_screen = [o_screens objectAtIndex: i_index];
1386 config_PutInt( p_vout, "macosx-vdev", i_index );
1387 b_main_screen = (i_index == 0);
1391 if( p_vout->b_fullscreen )
1393 NSRect screen_rect = [o_screen frame];
1394 screen_rect.origin.x = screen_rect.origin.y = 0;
1396 if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1397 BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1398 NULL, NULL, fullScreenAllowEvents );
1400 [p_vout->p_sys->o_window
1401 initWithContentRect: screen_rect
1402 styleMask: NSBorderlessWindowMask
1403 backing: NSBackingStoreBuffered
1404 defer: NO screen: o_screen];
1406 [p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1407 p_vout->p_sys->b_mouse_moved = YES;
1408 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1412 unsigned int i_stylemask = NSTitledWindowMask |
1413 NSMiniaturizableWindowMask |
1414 NSClosableWindowMask |
1415 NSResizableWindowMask;
1417 if ( p_vout->p_sys->p_fullscreen_state != NULL )
1418 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1419 p_vout->p_sys->p_fullscreen_state = NULL;
1421 [p_vout->p_sys->o_window
1422 initWithContentRect: p_vout->p_sys->s_rect
1423 styleMask: i_stylemask
1424 backing: NSBackingStoreBuffered
1425 defer: NO screen: o_screen];
1427 [p_vout->p_sys->o_window setAlphaValue: config_GetFloat( p_vout, "macosx-opaqueness" )];
1429 if( config_GetInt( p_vout, "macosx-float" ) )
1431 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
1434 if( !p_vout->p_sys->b_pos_saved )
1436 [p_vout->p_sys->o_window center];
1440 o_view = [[VLCView alloc] init];
1442 /* FIXME: [o_view setMenu:] */
1443 [p_vout->p_sys->o_window setContentView: o_view];
1444 [o_view autorelease];
1447 p_vout->p_sys->p_qdport = [o_view qdPort];
1449 [o_view unlockFocus];
1451 [p_vout->p_sys->o_window updateTitle];
1452 [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1455 - (void)destroyWindow:(NSValue *)o_value
1457 vout_thread_t * p_vout;
1459 p_vout = (vout_thread_t *)[o_value pointerValue];
1461 if( !p_vout->b_fullscreen )
1465 s_rect = [[p_vout->p_sys->o_window contentView] frame];
1466 p_vout->p_sys->s_rect.size = s_rect.size;
1468 s_rect = [p_vout->p_sys->o_window frame];
1469 p_vout->p_sys->s_rect.origin = s_rect.origin;
1471 p_vout->p_sys->b_pos_saved = YES;
1474 p_vout->p_sys->p_qdport = nil;
1475 [p_vout->p_sys->o_window close];
1476 p_vout->p_sys->o_window = nil;