2 /*****************************************************************************
3 * vout.m: MacOS X video output plugin
4 *****************************************************************************
5 * Copyright (C) 2001-2003 VideoLAN
6 * $Id: vout.m,v 1.62 2003/11/05 00:40:08 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_pointer_visible = VLC_TRUE;
147 p_vout->p_sys->b_mouse_moved = VLC_TRUE;
148 p_vout->p_sys->i_time_mouse_last_moved = mdate();
150 if( value_drawable.i_int != 0 )
152 p_vout->p_sys->mask = NewRgn();
153 p_vout->p_sys->rect.left = 0 ;
154 p_vout->p_sys->rect.right = 0 ;
155 p_vout->p_sys->rect.top = 0 ;
156 p_vout->p_sys->rect.bottom = 0 ;
158 p_vout->p_sys->isplugin = VLC_TRUE ;
162 p_vout->p_sys->mask = NULL;
163 p_vout->p_sys->isplugin = VLC_FALSE ;
166 /* set window size */
167 p_vout->p_sys->s_rect.size.width = p_vout->i_window_width;
168 p_vout->p_sys->s_rect.size.height = p_vout->i_window_height;
170 if( ( err = EnterMovies() ) != noErr )
172 msg_Err( p_vout, "EnterMovies failed: %d", err );
173 free( p_vout->p_sys->p_matrix );
174 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
175 free( p_vout->p_sys );
179 /* Damn QT isn't thread safe. so keep a lock in the p_vlc object */
180 vlc_mutex_lock( &p_vout->p_vlc->quicktime_lock );
182 err = FindCodec( kYUV420CodecType, bestSpeedCodec,
183 nil, &p_vout->p_sys->img_dc );
185 vlc_mutex_unlock( &p_vout->p_vlc->quicktime_lock );
186 if( err == noErr && p_vout->p_sys->img_dc != 0 )
188 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
189 p_vout->p_sys->i_codec = kYUV420CodecType;
193 msg_Err( p_vout, "failed to find an appropriate codec" );
196 if( p_vout->p_sys->img_dc == 0 )
198 free( p_vout->p_sys->p_matrix );
199 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
200 free( p_vout->p_sys );
204 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
205 NSArray * o_screens = [NSScreen screens];
206 if( [o_screens count] > 0 && var_Type( p_vout, "video-device" ) == 0 )
209 vlc_value_t val, text;
212 int i_option = config_GetInt( p_vout, "macosx-vdev" );
214 var_Create( p_vout, "video-device", VLC_VAR_INTEGER |
216 text.psz_string = _("Video device");
217 var_Change( p_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL );
219 NSEnumerator * o_enumerator = [o_screens objectEnumerator];
221 while( (o_screen = [o_enumerator nextObject]) != NULL )
224 NSRect s_rect = [o_screen frame];
226 snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
227 "%s %d (%dx%d)", _("Screen"), i,
228 (int)s_rect.size.width, (int)s_rect.size.height );
230 text.psz_string = psz_temp;
232 var_Change( p_vout, "video-device",
233 VLC_VAR_ADDCHOICE, &val, &text );
235 if( ( i - 1 ) == i_option )
237 var_Set( p_vout, "video-device", val );
242 var_AddCallback( p_vout, "video-device", vout_VarCallback,
245 val.b_bool = VLC_TRUE;
246 var_Set( p_vout, "intf-change", val );
250 /* We don't need a window either in the mozilla plugin */
251 if( p_vout->p_sys->isplugin == 0 )
253 if( CoCreateWindow( p_vout ) )
255 msg_Err( p_vout, "unable to create window" );
256 free( p_vout->p_sys->p_matrix );
257 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
258 free( p_vout->p_sys );
263 p_vout->pf_init = vout_Init;
264 p_vout->pf_end = vout_End;
265 p_vout->pf_manage = vout_Manage;
266 p_vout->pf_render = NULL;
267 p_vout->pf_display = vout_Display;
272 /*****************************************************************************
273 * vout_Init: initialize video thread output method
274 *****************************************************************************/
275 static int vout_Init( vout_thread_t *p_vout )
281 I_OUTPUTPICTURES = 0;
283 /* Initialize the output structure; we already found a codec,
284 * and the corresponding chroma we will be using. Since we can
285 * arbitrary scale, stick to the coordinates and aspect. */
286 p_vout->output.i_width = p_vout->render.i_width;
287 p_vout->output.i_height = p_vout->render.i_height;
288 p_vout->output.i_aspect = p_vout->render.i_aspect;
290 var_Get( p_vout->p_vlc, "drawable", &val );
292 if( p_vout->p_sys->isplugin )
294 p_vout->p_sys->p_qdport = val.i_int;
297 SetPort( p_vout->p_sys->p_qdport );
298 QTScaleMatrix( p_vout );
300 if( QTCreateSequence( p_vout ) )
302 msg_Err( p_vout, "unable to create sequence" );
306 /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
307 while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
311 /* Find an empty picture slot */
312 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
314 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
316 p_pic = p_vout->p_picture + i_index;
321 /* Allocate the picture */
322 if( p_pic == NULL || QTNewPicture( p_vout, p_pic ) )
327 p_pic->i_status = DESTROYED_PICTURE;
328 p_pic->i_type = DIRECT_PICTURE;
330 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
338 /*****************************************************************************
339 * vout_End: terminate video thread output method
340 *****************************************************************************/
341 static void vout_End( vout_thread_t *p_vout )
345 QTDestroySequence( p_vout );
347 /* Free the direct buffers we allocated */
348 for( i_index = I_OUTPUTPICTURES; i_index; )
351 QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
355 /*****************************************************************************
356 * CloseVideo: destroy video thread output method
357 *****************************************************************************/
358 void E_(CloseVideo) ( vlc_object_t *p_this )
360 vout_thread_t * p_vout = (vout_thread_t *)p_this;
362 if ( !p_vout->p_sys->isplugin )
364 if( CoDestroyWindow( p_vout ) )
366 msg_Err( p_vout, "unable to destroy window" );
370 if ( p_vout->p_sys->p_fullscreen_state != NULL )
371 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
375 free( p_vout->p_sys->p_matrix );
376 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
378 free( p_vout->p_sys );
381 /*****************************************************************************
382 * vout_Manage: handle events
383 *****************************************************************************
384 * This function should be called regularly by video output thread. It manages
385 * console events. It returns a non null value on error.
386 *****************************************************************************/
387 static int vout_Manage( vout_thread_t *p_vout )
390 var_Get( p_vout->p_vlc, "drawableredraw", &val1 );
392 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
394 if( CoToggleFullscreen( p_vout ) )
399 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
402 if( (p_vout->i_changes & VOUT_SIZE_CHANGE) ||
403 ( p_vout->p_sys->isplugin && val1.i_int == 1) )
405 if( p_vout->p_sys->isplugin )
408 var_Set( p_vout->p_vlc, "drawableredraw", val1 );
409 QTScaleMatrix( p_vout );
410 SetDSequenceMask( p_vout->p_sys->i_seq , p_vout->p_sys->mask );
414 QTScaleMatrix( p_vout );
415 SetDSequenceMatrix( p_vout->p_sys->i_seq,
416 p_vout->p_sys->p_matrix );
417 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
421 /* hide/show mouse cursor
422 * this code looks unnecessarily complicated, but is necessary like this.
423 * it has to deal with multiple monitors and therefore checks a lot */
424 if( !p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
426 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 2000000 &&
427 p_vout->p_sys->b_mouse_pointer_visible )
429 VLCHideMouse( p_vout, YES );
431 else if ( !p_vout->p_sys->b_mouse_pointer_visible )
433 vlc_bool_t b_playing = NO;
434 input_thread_t * p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
437 if ( p_input != NULL )
440 var_Get( p_input, "state", &state );
441 b_playing = state.i_int != PAUSE_S;
442 vlc_object_release( p_input );
446 VLCHideMouse( p_vout, NO );
450 else if ( p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
452 if( !p_vout->p_sys->b_mouse_pointer_visible )
454 VLCHideMouse( p_vout, NO );
458 p_vout->p_sys->b_mouse_moved = NO;
465 /*****************************************************************************
466 * vout_Display: displays previously rendered output
467 *****************************************************************************
468 * This function sends the currently rendered image to the display.
469 *****************************************************************************/
470 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
477 if( p_vout->p_sys->isplugin )
481 /* In mozilla plugin, mozilla browser also draws things in
482 * the windows. So we have to update the port/Origin for each
483 * picture. FIXME : the vout should lock something ! */
484 GetPort( &p_vout->p_sys->p_qdportold );
485 GetPortBounds( p_vout->p_sys->p_qdportold, &oldrect );
488 LockPortBits( p_vout->p_sys->p_qdport );
490 SetPort( p_vout->p_sys->p_qdport );
491 SetOrigin( p_vout->p_sys->portx , p_vout->p_sys->porty );
492 ClipRect( &p_vout->p_sys->rect );
495 if( ( err = DecompressSequenceFrameS(
496 p_vout->p_sys->i_seq,
497 p_pic->p_sys->p_info,
498 p_pic->p_sys->i_size,
499 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
501 msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
505 QDFlushPortBuffer( p_vout->p_sys->p_qdport, p_vout->p_sys->mask );
509 SetOrigin( oldrect.left , oldrect.top );
511 SetPort( p_vout->p_sys->p_qdportold );
513 UnlockPortBits( p_vout->p_sys->p_qdport );
518 if( ( err = DecompressSequenceFrameS(
519 p_vout->p_sys->i_seq,
520 p_pic->p_sys->p_info,
521 p_pic->p_sys->i_size,
522 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
524 msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
528 QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
533 /*****************************************************************************
534 * CoSendRequest: send request to interface thread
535 *****************************************************************************
536 * Returns 0 on success, 1 otherwise
537 *****************************************************************************/
538 static int CoSendRequest( vout_thread_t *p_vout, SEL sel )
542 VLCVout * o_vlv = [[VLCVout alloc] init];
544 if( ( i_ret = ExecuteOnMainThread( o_vlv, sel, (void *)p_vout ) ) )
546 msg_Err( p_vout, "SendRequest: no way to communicate with mt" );
554 /*****************************************************************************
555 * CoCreateWindow: create new window
556 *****************************************************************************
557 * Returns 0 on success, 1 otherwise
558 *****************************************************************************/
559 static int CoCreateWindow( vout_thread_t *p_vout )
561 if( CoSendRequest( p_vout, @selector(createWindow:) ) )
563 msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
570 /*****************************************************************************
571 * CoDestroyWindow: destroy window
572 *****************************************************************************
573 * Returns 0 on success, 1 otherwise
574 *****************************************************************************/
575 static int CoDestroyWindow( vout_thread_t *p_vout )
577 if( !p_vout->p_sys->b_mouse_pointer_visible )
579 VLCHideMouse( p_vout, NO );
582 if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
584 msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
591 /*****************************************************************************
592 * CoToggleFullscreen: toggle fullscreen
593 *****************************************************************************
594 * Returns 0 on success, 1 otherwise
595 *****************************************************************************/
596 static int CoToggleFullscreen( vout_thread_t *p_vout )
598 QTDestroySequence( p_vout );
600 if( CoDestroyWindow( p_vout ) )
602 msg_Err( p_vout, "unable to destroy window" );
606 p_vout->b_fullscreen = !p_vout->b_fullscreen;
608 config_PutInt( p_vout, "fullscreen", p_vout->b_fullscreen );
610 if( CoCreateWindow( p_vout ) )
612 msg_Err( p_vout, "unable to create window" );
616 if( QTCreateSequence( p_vout ) )
618 msg_Err( p_vout, "unable to create sequence" );
625 /*****************************************************************************
626 * VLCHideMouse: if b_hide then hide the cursor
627 *****************************************************************************/
628 static void VLCHideMouse ( vout_thread_t *p_vout, BOOL b_hide )
633 NSWindow *o_window = p_vout->p_sys->o_window;
634 NSView *o_contents = [o_window contentView];
636 s_rect = [o_contents bounds];
637 ml = [o_window convertScreenToBase:[NSEvent mouseLocation]];
638 ml = [o_contents convertPoint:ml fromView:nil];
639 b_inside = [o_contents mouse: ml inRect: s_rect];
641 if ( b_hide && b_inside )
643 /* only hide if mouse over VLCView */
645 p_vout->p_sys->b_mouse_pointer_visible = 0;
650 p_vout->p_sys->b_mouse_pointer_visible = 1;
652 p_vout->p_sys->b_mouse_moved = NO;
653 p_vout->p_sys->i_time_mouse_last_moved = mdate();
657 /*****************************************************************************
658 * QTScaleMatrix: scale matrix
659 *****************************************************************************/
660 static void QTScaleMatrix( vout_thread_t *p_vout )
663 unsigned int i_width, i_height;
664 Fixed factor_x, factor_y;
665 unsigned int i_offset_x = 0;
666 unsigned int i_offset_y = 0;
676 vlc_value_t valportx;
677 vlc_value_t valporty;
679 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
680 i_width = s_rect.right - s_rect.left;
681 i_height = s_rect.bottom - s_rect.top;
683 var_Get( p_vout->p_vlc, "drawable", &val );
684 var_Get( p_vout->p_vlc, "drawablet", &valt );
685 var_Get( p_vout->p_vlc, "drawablel", &vall );
686 var_Get( p_vout->p_vlc, "drawableb", &valb );
687 var_Get( p_vout->p_vlc, "drawabler", &valr );
688 var_Get( p_vout->p_vlc, "drawablex", &valx );
689 var_Get( p_vout->p_vlc, "drawabley", &valy );
690 var_Get( p_vout->p_vlc, "drawablew", &valw );
691 var_Get( p_vout->p_vlc, "drawableh", &valh );
692 var_Get( p_vout->p_vlc, "drawableportx", &valportx );
693 var_Get( p_vout->p_vlc, "drawableporty", &valporty );
695 if( p_vout->p_sys->isplugin )
697 p_vout->p_sys->portx = valportx.i_int;
698 p_vout->p_sys->porty = valporty.i_int;
699 p_vout->p_sys->p_qdport = val.i_int;
700 i_width = valw.i_int;
701 i_height = valh.i_int;
703 SetRectRgn( p_vout->p_sys->mask , vall.i_int - valx.i_int ,
704 valt.i_int - valy.i_int , valr.i_int - valx.i_int ,
705 valb.i_int - valy.i_int );
706 p_vout->p_sys->rect.top = 0;
707 p_vout->p_sys->rect.left = 0;
708 p_vout->p_sys->rect.bottom = valb.i_int - valt.i_int;
709 p_vout->p_sys->rect.right = valr.i_int - vall.i_int;
712 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
714 int i_adj_width = i_height * p_vout->output.i_aspect /
717 factor_x = FixDiv( Long2Fix( i_adj_width ),
718 Long2Fix( p_vout->output.i_width ) );
719 factor_y = FixDiv( Long2Fix( i_height ),
720 Long2Fix( p_vout->output.i_height ) );
722 i_offset_x = (i_width - i_adj_width) / 2 + i_offset_x;
726 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
727 p_vout->output.i_aspect;
729 factor_x = FixDiv( Long2Fix( i_width ),
730 Long2Fix( p_vout->output.i_width ) );
731 factor_y = FixDiv( Long2Fix( i_adj_height ),
732 Long2Fix( p_vout->output.i_height ) );
734 i_offset_y = (i_height - i_adj_height) / 2 + i_offset_y;
737 SetIdentityMatrix( p_vout->p_sys->p_matrix );
739 ScaleMatrix( p_vout->p_sys->p_matrix,
741 Long2Fix(0), Long2Fix(0) );
743 TranslateMatrix( p_vout->p_sys->p_matrix,
744 Long2Fix(i_offset_x),
745 Long2Fix(i_offset_y) );
749 /*****************************************************************************
750 * QTCreateSequence: create a new sequence
751 *****************************************************************************
752 * Returns 0 on success, 1 otherwise
753 *****************************************************************************/
754 static int QTCreateSequence( vout_thread_t *p_vout )
757 ImageDescriptionPtr p_descr;
759 HLock( (Handle)p_vout->p_sys->h_img_descr );
760 p_descr = *p_vout->p_sys->h_img_descr;
762 p_descr->idSize = sizeof(ImageDescription);
763 p_descr->cType = p_vout->p_sys->i_codec;
764 p_descr->version = 1;
765 p_descr->revisionLevel = 0;
766 p_descr->vendor = 'appl';
767 p_descr->width = p_vout->output.i_width;
768 p_descr->height = p_vout->output.i_height;
769 p_descr->hRes = Long2Fix(72);
770 p_descr->vRes = Long2Fix(72);
771 p_descr->spatialQuality = codecLosslessQuality;
772 p_descr->frameCount = 1;
773 p_descr->clutID = -1;
774 p_descr->dataSize = 0;
778 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
781 if( ( err = DecompressSequenceBeginS(
782 &p_vout->p_sys->i_seq,
783 p_vout->p_sys->h_img_descr,
785 p_vout->p_sys->p_qdport,
787 p_vout->p_sys->p_matrix,
788 0, p_vout->p_sys->mask,
789 codecFlagUseImageBuffer,
790 codecLosslessQuality,
791 p_vout->p_sys->img_dc ) ) )
793 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
801 /*****************************************************************************
802 * QTDestroySequence: destroy sequence
803 *****************************************************************************/
804 static void QTDestroySequence( vout_thread_t *p_vout )
806 CDSequenceEnd( p_vout->p_sys->i_seq );
809 /*****************************************************************************
810 * QTNewPicture: allocate a picture
811 *****************************************************************************
812 * Returns 0 on success, 1 otherwise
813 *****************************************************************************/
814 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
816 int i_width = p_vout->output.i_width;
817 int i_height = p_vout->output.i_height;
819 /* We know the chroma, allocate a buffer which will be used
820 * directly by the decoder */
821 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
823 if( p_pic->p_sys == NULL )
828 switch( p_vout->output.i_chroma )
830 case VLC_FOURCC('I','4','2','0'):
832 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
833 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
835 /* Allocate the memory buffer */
836 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
837 16, i_width * i_height * 3 / 2 );
840 p_pic->Y_PIXELS = p_pic->p_data;
841 p_pic->p[Y_PLANE].i_lines = i_height;
842 p_pic->p[Y_PLANE].i_pitch = i_width;
843 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
844 p_pic->p[Y_PLANE].i_visible_pitch = i_width;
847 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
848 p_pic->p[U_PLANE].i_lines = i_height / 2;
849 p_pic->p[U_PLANE].i_pitch = i_width / 2;
850 p_pic->p[U_PLANE].i_pixel_pitch = 1;
851 p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
854 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
855 p_pic->p[V_PLANE].i_lines = i_height / 2;
856 p_pic->p[V_PLANE].i_pitch = i_width / 2;
857 p_pic->p[V_PLANE].i_pixel_pitch = 1;
858 p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
860 /* We allocated 3 planes */
863 #define P p_pic->p_sys->pixmap_i420
864 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
865 - p_pic->p_sys->p_info;
866 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
867 - p_pic->p_sys->p_info;
868 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
869 - p_pic->p_sys->p_info;
871 P.componentInfoY.rowBytes = i_width;
872 P.componentInfoCb.rowBytes = i_width / 2;
873 P.componentInfoCr.rowBytes = i_width / 2;
879 /* Unknown chroma, tell the guy to get lost */
880 free( p_pic->p_sys );
881 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
882 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
890 /*****************************************************************************
891 * QTFreePicture: destroy a picture allocated with QTNewPicture
892 *****************************************************************************/
893 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
895 switch( p_vout->output.i_chroma )
897 case VLC_FOURCC('I','4','2','0'):
898 free( p_pic->p_data_orig );
902 free( p_pic->p_sys );
905 /*****************************************************************************
906 * VLCWindow implementation
907 *****************************************************************************/
908 @implementation VLCWindow
910 - (void)setVout:(vout_thread_t *)_p_vout
915 - (vout_thread_t *)getVout
920 - (void)scaleWindowWithFactor: (float)factor
923 int i_corrected_height, i_corrected_width;
925 NSPoint topleftscreen;
927 if ( !p_vout->b_fullscreen )
930 topleftbase.y = [self frame].size.height;
931 topleftscreen = [self convertBaseToScreen: topleftbase];
933 if( p_vout->output.i_height * p_vout->output.i_aspect >
934 p_vout->output.i_width * VOUT_ASPECT_FACTOR )
936 i_corrected_width = p_vout->output.i_height * p_vout->output.i_aspect /
938 newsize.width = (int) ( i_corrected_width * factor );
939 newsize.height = (int) ( p_vout->render.i_height * factor );
943 i_corrected_height = p_vout->output.i_width * VOUT_ASPECT_FACTOR /
944 p_vout->output.i_aspect;
945 newsize.width = (int) ( p_vout->render.i_width * factor );
946 newsize.height = (int) ( i_corrected_height * factor );
949 [self setContentSize: newsize];
951 [self setFrameTopLeftPoint: topleftscreen];
952 p_vout->i_changes |= VOUT_SIZE_CHANGE;
956 - (void)toggleFloatOnTop
958 if( config_GetInt( p_vout, "macosx-float" ) )
960 config_PutInt( p_vout, "macosx-float", 0 );
961 [p_vout->p_sys->o_window setLevel: NSNormalWindowLevel];
965 config_PutInt( p_vout, "macosx-float", 1 );
966 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
970 - (void)toggleFullscreen
972 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
977 return( p_vout->b_fullscreen );
980 - (BOOL)canBecomeKeyWindow
985 - (void)keyDown:(NSEvent *)o_event
989 unsigned int i_pressed_modifiers = 0;
992 i_pressed_modifiers = [o_event modifierFlags];
994 if( i_pressed_modifiers & NSShiftKeyMask )
995 val.i_int |= KEY_MODIFIER_SHIFT;
996 if( i_pressed_modifiers & NSControlKeyMask )
997 val.i_int |= KEY_MODIFIER_CTRL;
998 if( i_pressed_modifiers & NSAlternateKeyMask )
999 val.i_int |= KEY_MODIFIER_ALT;
1000 if( i_pressed_modifiers & NSCommandKeyMask )
1001 val.i_int |= KEY_MODIFIER_COMMAND;
1003 key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
1007 val.i_int |= CocoaConvertKey( key );
1008 var_Set( p_vout->p_vlc, "key-pressed", val );
1012 [super keyDown: o_event];
1018 NSMutableString * o_title;
1019 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1022 if( p_playlist == NULL )
1027 vlc_mutex_lock( &p_playlist->object_lock );
1028 o_title = [NSMutableString stringWithUTF8String:
1029 p_playlist->pp_items[p_playlist->i_index]->psz_uri];
1030 vlc_mutex_unlock( &p_playlist->object_lock );
1032 vlc_object_release( p_playlist );
1034 if( o_title != nil )
1036 NSRange prefix_range = [o_title rangeOfString: @"file:"];
1037 if( prefix_range.location != NSNotFound )
1039 [o_title deleteCharactersInRange: prefix_range];
1042 [self setTitleWithRepresentedFilename: o_title];
1047 [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
1051 /* This is actually the same as VLCControls::stop. */
1052 - (BOOL)windowShouldClose:(id)sender
1054 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1056 if( p_playlist == NULL )
1061 playlist_Stop( p_playlist );
1062 vlc_object_release( p_playlist );
1064 /* The window will be closed by the intf later. */
1070 /*****************************************************************************
1071 * VLCView implementation
1072 *****************************************************************************/
1073 @implementation VLCView
1075 - (void)drawRect:(NSRect)rect
1077 vout_thread_t * p_vout;
1078 id o_window = [self window];
1079 p_vout = (vout_thread_t *)[o_window getVout];
1081 [[NSColor blackColor] set];
1083 [super drawRect: rect];
1085 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1088 - (BOOL)acceptsFirstResponder
1093 - (BOOL)becomeFirstResponder
1095 vout_thread_t * p_vout;
1096 id o_window = [self window];
1097 p_vout = (vout_thread_t *)[o_window getVout];
1099 [o_window setAcceptsMouseMovedEvents: YES];
1103 - (BOOL)resignFirstResponder
1105 vout_thread_t * p_vout;
1106 id o_window = [self window];
1107 p_vout = (vout_thread_t *)[o_window getVout];
1109 [o_window setAcceptsMouseMovedEvents: NO];
1110 VLCHideMouse( p_vout, NO );
1114 - (void)mouseDown:(NSEvent *)o_event
1116 vout_thread_t * p_vout;
1117 id o_window = [self window];
1118 p_vout = (vout_thread_t *)[o_window getVout];
1121 switch( [o_event type] )
1123 case NSLeftMouseDown:
1125 var_Get( p_vout, "mouse-button-down", &val );
1127 var_Set( p_vout, "mouse-button-down", val );
1132 [super mouseDown: o_event];
1137 - (void)otherMouseDown:(NSEvent *)o_event
1139 /* This is not the the wheel button. you need to poll the
1140 * mouseWheel event for that. other is a third, forth or fifth button */
1141 vout_thread_t * p_vout;
1142 id o_window = [self window];
1143 p_vout = (vout_thread_t *)[o_window getVout];
1146 switch( [o_event type] )
1148 case NSOtherMouseDown:
1150 var_Get( p_vout, "mouse-button-down", &val );
1152 var_Set( p_vout, "mouse-button-down", val );
1157 [super mouseDown: o_event];
1162 - (void)rightMouseDown:(NSEvent *)o_event
1164 vout_thread_t * p_vout;
1165 id o_window = [self window];
1166 p_vout = (vout_thread_t *)[o_window getVout];
1169 switch( [o_event type] )
1171 case NSRightMouseDown:
1173 var_Get( p_vout, "mouse-button-down", &val );
1175 var_Set( p_vout, "mouse-button-down", val );
1180 [super mouseDown: o_event];
1185 - (void)mouseUp:(NSEvent *)o_event
1187 vout_thread_t * p_vout;
1188 id o_window = [self window];
1189 p_vout = (vout_thread_t *)[o_window getVout];
1192 switch( [o_event type] )
1197 b_val.b_bool = VLC_TRUE;
1198 var_Set( p_vout, "mouse-clicked", b_val );
1200 var_Get( p_vout, "mouse-button-down", &val );
1202 var_Set( p_vout, "mouse-button-down", val );
1207 [super mouseUp: o_event];
1212 - (void)otherMouseUp:(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] )
1221 case NSOtherMouseUp:
1223 var_Get( p_vout, "mouse-button-down", &val );
1225 var_Set( p_vout, "mouse-button-down", val );
1230 [super mouseUp: o_event];
1235 - (void)rightMouseUp:(NSEvent *)o_event
1237 vout_thread_t * p_vout;
1238 id o_window = [self window];
1239 p_vout = (vout_thread_t *)[o_window getVout];
1242 switch( [o_event type] )
1244 case NSRightMouseUp:
1246 var_Get( p_vout, "mouse-button-down", &val );
1248 var_Set( p_vout, "mouse-button-down", val );
1253 [super mouseUp: o_event];
1258 - (void)mouseDragged:(NSEvent *)o_event
1260 [self mouseMoved:o_event];
1263 - (void)otherMouseDragged:(NSEvent *)o_event
1265 [self mouseMoved:o_event];
1268 - (void)rightMouseDragged:(NSEvent *)o_event
1270 [self mouseMoved:o_event];
1273 - (void)mouseMoved:(NSEvent *)o_event
1279 vout_thread_t * p_vout;
1280 id o_window = [self window];
1281 p_vout = (vout_thread_t *)[o_window getVout];
1283 s_rect = [self bounds];
1284 ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1285 b_inside = [self mouse: ml inRect: s_rect];
1290 int i_width, i_height, i_x, i_y;
1292 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1293 (unsigned int)s_rect.size.height,
1294 &i_x, &i_y, &i_width, &i_height );
1296 val.i_int = ( ((int)ml.x) - i_x ) *
1297 p_vout->render.i_width / i_width;
1298 var_Set( p_vout, "mouse-x", val );
1300 val.i_int = ( ((int)ml.y) - i_y ) *
1301 p_vout->render.i_height / i_height;
1302 var_Set( p_vout, "mouse-y", val );
1304 val.b_bool = VLC_TRUE;
1305 var_Set( p_vout, "mouse-moved", val );
1306 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1307 p_vout->p_sys->b_mouse_moved = YES;
1309 else if ( !b_inside && !p_vout->p_sys->b_mouse_pointer_visible )
1311 /* people with multiple monitors need their mouse,
1312 * even if VLCView in fullscreen. */
1313 VLCHideMouse( p_vout, NO );
1316 [super mouseMoved: o_event];
1321 /*****************************************************************************
1322 * VLCVout implementation
1323 *****************************************************************************/
1324 @implementation VLCVout
1326 - (void)createWindow:(NSValue *)o_value
1330 NSScreen * o_screen;
1331 vout_thread_t * p_vout;
1332 vlc_bool_t b_main_screen;
1334 p_vout = (vout_thread_t *)[o_value pointerValue];
1336 p_vout->p_sys->o_window = [VLCWindow alloc];
1337 [p_vout->p_sys->o_window setVout: p_vout];
1338 [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1340 if( var_Get( p_vout, "video-device", &val ) < 0 )
1342 o_screen = [NSScreen mainScreen];
1347 NSArray *o_screens = [NSScreen screens];
1348 unsigned int i_index = val.i_int;
1350 if( [o_screens count] < i_index )
1352 o_screen = [NSScreen mainScreen];
1358 o_screen = [o_screens objectAtIndex: i_index];
1359 config_PutInt( p_vout, "macosx-vdev", i_index );
1360 b_main_screen = (i_index == 0);
1364 if( p_vout->b_fullscreen )
1366 NSRect screen_rect = [o_screen frame];
1367 screen_rect.origin.x = screen_rect.origin.y = 0;
1369 if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1370 BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1371 NULL, NULL, fullScreenAllowEvents );
1373 [p_vout->p_sys->o_window
1374 initWithContentRect: screen_rect
1375 styleMask: NSBorderlessWindowMask
1376 backing: NSBackingStoreBuffered
1377 defer: NO screen: o_screen];
1379 //[p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1380 p_vout->p_sys->b_mouse_moved = YES;
1381 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1385 unsigned int i_stylemask = NSTitledWindowMask |
1386 NSMiniaturizableWindowMask |
1387 NSClosableWindowMask |
1388 NSResizableWindowMask;
1390 if ( p_vout->p_sys->p_fullscreen_state != NULL )
1391 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1392 p_vout->p_sys->p_fullscreen_state = NULL;
1394 [p_vout->p_sys->o_window
1395 initWithContentRect: p_vout->p_sys->s_rect
1396 styleMask: i_stylemask
1397 backing: NSBackingStoreBuffered
1398 defer: NO screen: o_screen];
1400 [p_vout->p_sys->o_window setAlphaValue: config_GetFloat( p_vout, "macosx-opaqueness" )];
1402 if( config_GetInt( p_vout, "macosx-float" ) )
1404 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
1407 if( !p_vout->p_sys->b_pos_saved )
1409 [p_vout->p_sys->o_window center];
1413 o_view = [[VLCView alloc] init];
1415 /* FIXME: [o_view setMenu:] */
1416 [p_vout->p_sys->o_window setContentView: o_view];
1417 [o_view autorelease];
1420 p_vout->p_sys->p_qdport = [o_view qdPort];
1422 [o_view unlockFocus];
1424 [p_vout->p_sys->o_window updateTitle];
1425 [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1428 - (void)destroyWindow:(NSValue *)o_value
1430 vout_thread_t * p_vout;
1432 p_vout = (vout_thread_t *)[o_value pointerValue];
1434 if( !p_vout->b_fullscreen )
1438 s_rect = [[p_vout->p_sys->o_window contentView] frame];
1439 p_vout->p_sys->s_rect.size = s_rect.size;
1441 s_rect = [p_vout->p_sys->o_window frame];
1442 p_vout->p_sys->s_rect.origin = s_rect.origin;
1444 p_vout->p_sys->b_pos_saved = YES;
1447 p_vout->p_sys->p_qdport = nil;
1448 [p_vout->p_sys->o_window close];
1449 p_vout->p_sys->o_window = nil;