2 /*****************************************************************************
3 * vout.m: MacOS X video output plugin
4 *****************************************************************************
5 * Copyright (C) 2001-2003 VideoLAN
6 * $Id: vout.m,v 1.58 2003/10/29 02:13:04 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 = YES;
147 p_vout->p_sys->b_mouse_moved = YES;
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 = 1 ;
162 p_vout->p_sys->mask = NULL;
163 p_vout->p_sys->isplugin = 0 ;
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 SetDSequenceMask( p_vout->p_sys->i_seq , p_vout->p_sys->mask );
413 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
416 QTScaleMatrix( p_vout );
417 SetDSequenceMatrix( p_vout->p_sys->i_seq,
418 p_vout->p_sys->p_matrix );
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;
991 i_pressed_modifiers = GetCurrentKeyModifiers();
993 if( i_pressed_modifiers & NSShiftKeyMask )
994 val.i_int |= KEY_MODIFIER_SHIFT;
995 if( i_pressed_modifiers & NSControlKeyMask )
996 val.i_int |= KEY_MODIFIER_CTRL;
997 if( i_pressed_modifiers & NSAlternateKeyMask )
998 val.i_int |= KEY_MODIFIER_ALT;
999 if( i_pressed_modifiers & NSCommandKeyMask )
1000 val.i_int |= KEY_MODIFIER_COMMAND;
1002 NSLog( @"detected the modifiers: %x", val.i_int );
1004 key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
1008 val.i_int |= CocoaConvertKey( key );
1009 var_Set( p_vout->p_vlc, "key-pressed", val );
1010 NSLog( @"detected the key: %x", key );
1014 [super keyDown: o_event];
1020 NSMutableString * o_title;
1021 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1024 if( p_playlist == NULL )
1029 vlc_mutex_lock( &p_playlist->object_lock );
1030 o_title = [NSMutableString stringWithUTF8String:
1031 p_playlist->pp_items[p_playlist->i_index]->psz_name];
1032 vlc_mutex_unlock( &p_playlist->object_lock );
1034 vlc_object_release( p_playlist );
1036 if( o_title != nil )
1038 NSRange prefix_range = [o_title rangeOfString: @"file:"];
1039 if( prefix_range.location != NSNotFound )
1041 [o_title deleteCharactersInRange: prefix_range];
1044 [self setTitleWithRepresentedFilename: o_title];
1049 [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
1053 /* This is actually the same as VLCControls::stop. */
1054 - (BOOL)windowShouldClose:(id)sender
1056 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1058 if( p_playlist == NULL )
1063 playlist_Stop( p_playlist );
1064 vlc_object_release( p_playlist );
1066 /* The window will be closed by the intf later. */
1072 /*****************************************************************************
1073 * VLCView implementation
1074 *****************************************************************************/
1075 @implementation VLCView
1077 - (void)drawRect:(NSRect)rect
1079 vout_thread_t * p_vout;
1080 id o_window = [self window];
1081 p_vout = (vout_thread_t *)[o_window getVout];
1083 [[NSColor blackColor] set];
1085 [super drawRect: rect];
1087 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1090 - (BOOL)acceptsFirstResponder
1095 - (BOOL)becomeFirstResponder
1097 vout_thread_t * p_vout;
1098 id o_window = [self window];
1099 p_vout = (vout_thread_t *)[o_window getVout];
1101 [o_window setAcceptsMouseMovedEvents: YES];
1105 - (BOOL)resignFirstResponder
1107 vout_thread_t * p_vout;
1108 id o_window = [self window];
1109 p_vout = (vout_thread_t *)[o_window getVout];
1111 [o_window setAcceptsMouseMovedEvents: NO];
1112 VLCHideMouse( p_vout, NO );
1116 - (void)mouseDown:(NSEvent *)o_event
1118 vout_thread_t * p_vout;
1119 id o_window = [self window];
1120 p_vout = (vout_thread_t *)[o_window getVout];
1123 switch( [o_event type] )
1125 case NSLeftMouseDown:
1127 var_Get( p_vout, "mouse-button-down", &val );
1129 var_Set( p_vout, "mouse-button-down", val );
1134 [super mouseDown: o_event];
1139 - (void)otherMouseDown:(NSEvent *)o_event
1141 /* This is not the the wheel button. you need to poll the
1142 * mouseWheel event for that. other is a third, forth or fifth button */
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 NSOtherMouseDown:
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)rightMouseDown:(NSEvent *)o_event
1166 vout_thread_t * p_vout;
1167 id o_window = [self window];
1168 p_vout = (vout_thread_t *)[o_window getVout];
1171 switch( [o_event type] )
1173 case NSRightMouseDown:
1175 var_Get( p_vout, "mouse-button-down", &val );
1177 var_Set( p_vout, "mouse-button-down", val );
1182 [super mouseDown: o_event];
1187 - (void)mouseUp:(NSEvent *)o_event
1189 vout_thread_t * p_vout;
1190 id o_window = [self window];
1191 p_vout = (vout_thread_t *)[o_window getVout];
1194 switch( [o_event type] )
1199 b_val.b_bool = VLC_TRUE;
1200 var_Set( p_vout, "mouse-clicked", b_val );
1202 var_Get( p_vout, "mouse-button-down", &val );
1204 var_Set( p_vout, "mouse-button-down", val );
1209 [super mouseUp: o_event];
1214 - (void)otherMouseUp:(NSEvent *)o_event
1216 vout_thread_t * p_vout;
1217 id o_window = [self window];
1218 p_vout = (vout_thread_t *)[o_window getVout];
1221 switch( [o_event type] )
1223 case NSOtherMouseUp:
1225 var_Get( p_vout, "mouse-button-down", &val );
1227 var_Set( p_vout, "mouse-button-down", val );
1232 [super mouseUp: o_event];
1237 - (void)rightMouseUp:(NSEvent *)o_event
1239 vout_thread_t * p_vout;
1240 id o_window = [self window];
1241 p_vout = (vout_thread_t *)[o_window getVout];
1244 switch( [o_event type] )
1246 case NSRightMouseUp:
1248 var_Get( p_vout, "mouse-button-down", &val );
1250 var_Set( p_vout, "mouse-button-down", val );
1255 [super mouseUp: o_event];
1260 - (void)mouseDragged:(NSEvent *)o_event
1262 [self mouseMoved:o_event];
1265 - (void)otherMouseDragged:(NSEvent *)o_event
1267 [self mouseMoved:o_event];
1270 - (void)rightMouseDragged:(NSEvent *)o_event
1272 [self mouseMoved:o_event];
1275 - (void)mouseMoved:(NSEvent *)o_event
1281 vout_thread_t * p_vout;
1282 id o_window = [self window];
1283 p_vout = (vout_thread_t *)[o_window getVout];
1285 s_rect = [self bounds];
1286 ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1287 b_inside = [self mouse: ml inRect: s_rect];
1292 int i_width, i_height, i_x, i_y;
1294 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1295 (unsigned int)s_rect.size.height,
1296 &i_x, &i_y, &i_width, &i_height );
1298 val.i_int = ( ((int)ml.x) - i_x ) *
1299 p_vout->render.i_width / i_width;
1300 var_Set( p_vout, "mouse-x", val );
1302 val.i_int = ( ((int)ml.y) - i_y ) *
1303 p_vout->render.i_height / i_height;
1304 var_Set( p_vout, "mouse-y", val );
1306 val.b_bool = VLC_TRUE;
1307 var_Set( p_vout, "mouse-moved", val );
1308 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1309 p_vout->p_sys->b_mouse_moved = YES;
1311 else if ( !b_inside && !p_vout->p_sys->b_mouse_pointer_visible )
1313 /* people with multiple monitors need their mouse,
1314 * even if VLCView in fullscreen. */
1315 VLCHideMouse( p_vout, NO );
1318 [super mouseMoved: o_event];
1323 /*****************************************************************************
1324 * VLCVout implementation
1325 *****************************************************************************/
1326 @implementation VLCVout
1328 - (void)createWindow:(NSValue *)o_value
1332 NSScreen * o_screen;
1333 vout_thread_t * p_vout;
1334 vlc_bool_t b_main_screen;
1336 p_vout = (vout_thread_t *)[o_value pointerValue];
1338 p_vout->p_sys->o_window = [VLCWindow alloc];
1339 [p_vout->p_sys->o_window setVout: p_vout];
1340 [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1342 if( var_Get( p_vout, "video-device", &val ) < 0 )
1344 o_screen = [NSScreen mainScreen];
1349 NSArray *o_screens = [NSScreen screens];
1350 unsigned int i_index = val.i_int;
1352 if( [o_screens count] < i_index )
1354 o_screen = [NSScreen mainScreen];
1360 o_screen = [o_screens objectAtIndex: i_index];
1361 config_PutInt( p_vout, "macosx-vdev", i_index );
1362 b_main_screen = (i_index == 0);
1366 if( p_vout->b_fullscreen )
1368 NSRect screen_rect = [o_screen frame];
1369 screen_rect.origin.x = screen_rect.origin.y = 0;
1371 if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1372 BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1373 NULL, NULL, fullScreenAllowEvents );
1375 [p_vout->p_sys->o_window
1376 initWithContentRect: screen_rect
1377 styleMask: NSBorderlessWindowMask
1378 backing: NSBackingStoreBuffered
1379 defer: NO screen: o_screen];
1381 //[p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1382 p_vout->p_sys->b_mouse_moved = YES;
1383 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1387 unsigned int i_stylemask = NSTitledWindowMask |
1388 NSMiniaturizableWindowMask |
1389 NSClosableWindowMask |
1390 NSResizableWindowMask;
1392 if ( p_vout->p_sys->p_fullscreen_state != NULL )
1393 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1394 p_vout->p_sys->p_fullscreen_state = NULL;
1396 [p_vout->p_sys->o_window
1397 initWithContentRect: p_vout->p_sys->s_rect
1398 styleMask: i_stylemask
1399 backing: NSBackingStoreBuffered
1400 defer: NO screen: o_screen];
1402 [p_vout->p_sys->o_window setAlphaValue: config_GetFloat( p_vout, "macosx-opaqueness" )];
1404 if( config_GetInt( p_vout, "macosx-float" ) )
1406 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
1409 if( !p_vout->p_sys->b_pos_saved )
1411 [p_vout->p_sys->o_window center];
1415 o_view = [[VLCView alloc] init];
1417 /* FIXME: [o_view setMenu:] */
1418 [p_vout->p_sys->o_window setContentView: o_view];
1419 [o_view autorelease];
1422 p_vout->p_sys->p_qdport = [o_view qdPort];
1424 [o_view unlockFocus];
1426 [p_vout->p_sys->o_window updateTitle];
1427 [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1430 - (void)destroyWindow:(NSValue *)o_value
1432 vout_thread_t * p_vout;
1434 p_vout = (vout_thread_t *)[o_value pointerValue];
1436 if( !p_vout->b_fullscreen )
1440 s_rect = [[p_vout->p_sys->o_window contentView] frame];
1441 p_vout->p_sys->s_rect.size = s_rect.size;
1443 s_rect = [p_vout->p_sys->o_window frame];
1444 p_vout->p_sys->s_rect.origin = s_rect.origin;
1446 p_vout->p_sys->b_pos_saved = YES;
1449 p_vout->p_sys->p_qdport = nil;
1450 [p_vout->p_sys->o_window close];
1451 p_vout->p_sys->o_window = nil;