2 /*****************************************************************************
3 * vout.m: MacOS X video output plugin
4 *****************************************************************************
5 * Copyright (C) 2001-2003 VideoLAN
6 * $Id: vout.m,v 1.55 2003/08/25 14:51:47 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 LockPortBits( p_vout->p_sys->p_qdport );
491 SetPort( p_vout->p_sys->p_qdport );
492 SetOrigin( p_vout->p_sys->portx , p_vout->p_sys->porty );
493 ClipRect( &p_vout->p_sys->rect );
496 if( ( err = DecompressSequenceFrameS(
497 p_vout->p_sys->i_seq,
498 p_pic->p_sys->p_info,
499 p_pic->p_sys->i_size,
500 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
502 msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
506 QDFlushPortBuffer( p_vout->p_sys->p_qdport, p_vout->p_sys->mask );
510 SetOrigin( oldrect.left , oldrect.top );
512 SetPort( p_vout->p_sys->p_qdportold );
514 UnlockPortBits( p_vout->p_sys->p_qdport );
519 if( ( err = DecompressSequenceFrameS(
520 p_vout->p_sys->i_seq,
521 p_pic->p_sys->p_info,
522 p_pic->p_sys->i_size,
523 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
525 msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
529 QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
534 /*****************************************************************************
535 * CoSendRequest: send request to interface thread
536 *****************************************************************************
537 * Returns 0 on success, 1 otherwise
538 *****************************************************************************/
539 static int CoSendRequest( vout_thread_t *p_vout, SEL sel )
543 VLCVout * o_vlv = [[VLCVout alloc] init];
545 if( ( i_ret = ExecuteOnMainThread( o_vlv, sel, (void *)p_vout ) ) )
547 msg_Err( p_vout, "SendRequest: no way to communicate with mt" );
555 /*****************************************************************************
556 * CoCreateWindow: create new window
557 *****************************************************************************
558 * Returns 0 on success, 1 otherwise
559 *****************************************************************************/
560 static int CoCreateWindow( vout_thread_t *p_vout )
562 if( CoSendRequest( p_vout, @selector(createWindow:) ) )
564 msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
571 /*****************************************************************************
572 * CoDestroyWindow: destroy window
573 *****************************************************************************
574 * Returns 0 on success, 1 otherwise
575 *****************************************************************************/
576 static int CoDestroyWindow( vout_thread_t *p_vout )
578 if( !p_vout->p_sys->b_mouse_pointer_visible )
580 VLCHideMouse( p_vout, NO );
583 if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
585 msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
592 /*****************************************************************************
593 * CoToggleFullscreen: toggle fullscreen
594 *****************************************************************************
595 * Returns 0 on success, 1 otherwise
596 *****************************************************************************/
597 static int CoToggleFullscreen( vout_thread_t *p_vout )
599 QTDestroySequence( p_vout );
601 if( CoDestroyWindow( p_vout ) )
603 msg_Err( p_vout, "unable to destroy window" );
607 p_vout->b_fullscreen = !p_vout->b_fullscreen;
609 config_PutInt( p_vout, "fullscreen", p_vout->b_fullscreen );
611 if( CoCreateWindow( p_vout ) )
613 msg_Err( p_vout, "unable to create window" );
617 if( QTCreateSequence( p_vout ) )
619 msg_Err( p_vout, "unable to create sequence" );
626 /*****************************************************************************
627 * VLCHideMouse: if b_hide then hide the cursor
628 *****************************************************************************/
629 static void VLCHideMouse ( vout_thread_t *p_vout, BOOL b_hide )
634 NSWindow *o_window = p_vout->p_sys->o_window;
635 NSView *o_contents = [o_window contentView];
637 s_rect = [o_contents bounds];
638 ml = [o_window convertScreenToBase:[NSEvent mouseLocation]];
639 ml = [o_contents convertPoint:ml fromView:nil];
640 b_inside = [o_contents mouse: ml inRect: s_rect];
642 if ( b_hide && b_inside )
644 /* only hide if mouse over VLCView */
646 p_vout->p_sys->b_mouse_pointer_visible = 0;
651 p_vout->p_sys->b_mouse_pointer_visible = 1;
653 p_vout->p_sys->b_mouse_moved = NO;
654 p_vout->p_sys->i_time_mouse_last_moved = mdate();
658 /*****************************************************************************
659 * QTScaleMatrix: scale matrix
660 *****************************************************************************/
661 static void QTScaleMatrix( vout_thread_t *p_vout )
664 unsigned int i_width, i_height;
665 Fixed factor_x, factor_y;
666 unsigned int i_offset_x = 0;
667 unsigned int i_offset_y = 0;
677 vlc_value_t valportx;
678 vlc_value_t valporty;
680 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
681 i_width = s_rect.right - s_rect.left;
682 i_height = s_rect.bottom - s_rect.top;
684 var_Get( p_vout->p_vlc, "drawable", &val );
685 var_Get( p_vout->p_vlc, "drawablet", &valt );
686 var_Get( p_vout->p_vlc, "drawablel", &vall );
687 var_Get( p_vout->p_vlc, "drawableb", &valb );
688 var_Get( p_vout->p_vlc, "drawabler", &valr );
689 var_Get( p_vout->p_vlc, "drawablex", &valx );
690 var_Get( p_vout->p_vlc, "drawabley", &valy );
691 var_Get( p_vout->p_vlc, "drawablew", &valw );
692 var_Get( p_vout->p_vlc, "drawableh", &valh );
693 var_Get( p_vout->p_vlc, "drawableportx", &valportx );
694 var_Get( p_vout->p_vlc, "drawableporty", &valporty );
696 if( p_vout->p_sys->isplugin )
698 p_vout->p_sys->portx = valportx.i_int;
699 p_vout->p_sys->porty = valporty.i_int;
700 p_vout->p_sys->p_qdport = val.i_int;
701 i_width = valw.i_int;
702 i_height = valh.i_int;
704 SetRectRgn( p_vout->p_sys->mask , vall.i_int - valx.i_int ,
705 valt.i_int - valy.i_int , valr.i_int - valx.i_int ,
706 valb.i_int - valy.i_int );
707 p_vout->p_sys->rect.top = 0;
708 p_vout->p_sys->rect.left = 0;
709 p_vout->p_sys->rect.bottom = valb.i_int - valt.i_int;
710 p_vout->p_sys->rect.right = valr.i_int - vall.i_int;
713 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
715 int i_adj_width = i_height * p_vout->output.i_aspect /
718 factor_x = FixDiv( Long2Fix( i_adj_width ),
719 Long2Fix( p_vout->output.i_width ) );
720 factor_y = FixDiv( Long2Fix( i_height ),
721 Long2Fix( p_vout->output.i_height ) );
723 i_offset_x = (i_width - i_adj_width) / 2 + i_offset_x;
727 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
728 p_vout->output.i_aspect;
730 factor_x = FixDiv( Long2Fix( i_width ),
731 Long2Fix( p_vout->output.i_width ) );
732 factor_y = FixDiv( Long2Fix( i_adj_height ),
733 Long2Fix( p_vout->output.i_height ) );
735 i_offset_y = (i_height - i_adj_height) / 2 + i_offset_y;
738 SetIdentityMatrix( p_vout->p_sys->p_matrix );
740 ScaleMatrix( p_vout->p_sys->p_matrix,
742 Long2Fix(0), Long2Fix(0) );
744 TranslateMatrix( p_vout->p_sys->p_matrix,
745 Long2Fix(i_offset_x),
746 Long2Fix(i_offset_y) );
750 /*****************************************************************************
751 * QTCreateSequence: create a new sequence
752 *****************************************************************************
753 * Returns 0 on success, 1 otherwise
754 *****************************************************************************/
755 static int QTCreateSequence( vout_thread_t *p_vout )
758 ImageDescriptionPtr p_descr;
760 HLock( (Handle)p_vout->p_sys->h_img_descr );
761 p_descr = *p_vout->p_sys->h_img_descr;
763 p_descr->idSize = sizeof(ImageDescription);
764 p_descr->cType = p_vout->p_sys->i_codec;
765 p_descr->version = 1;
766 p_descr->revisionLevel = 0;
767 p_descr->vendor = 'appl';
768 p_descr->width = p_vout->output.i_width;
769 p_descr->height = p_vout->output.i_height;
770 p_descr->hRes = Long2Fix(72);
771 p_descr->vRes = Long2Fix(72);
772 p_descr->spatialQuality = codecLosslessQuality;
773 p_descr->frameCount = 1;
774 p_descr->clutID = -1;
775 p_descr->dataSize = 0;
779 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
782 if( ( err = DecompressSequenceBeginS(
783 &p_vout->p_sys->i_seq,
784 p_vout->p_sys->h_img_descr,
786 p_vout->p_sys->p_qdport,
788 p_vout->p_sys->p_matrix,
789 0, p_vout->p_sys->mask,
790 codecFlagUseImageBuffer,
791 codecLosslessQuality,
792 p_vout->p_sys->img_dc ) ) )
794 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
802 /*****************************************************************************
803 * QTDestroySequence: destroy sequence
804 *****************************************************************************/
805 static void QTDestroySequence( vout_thread_t *p_vout )
807 CDSequenceEnd( p_vout->p_sys->i_seq );
810 /*****************************************************************************
811 * QTNewPicture: allocate a picture
812 *****************************************************************************
813 * Returns 0 on success, 1 otherwise
814 *****************************************************************************/
815 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
817 int i_width = p_vout->output.i_width;
818 int i_height = p_vout->output.i_height;
820 /* We know the chroma, allocate a buffer which will be used
821 * directly by the decoder */
822 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
824 if( p_pic->p_sys == NULL )
829 switch( p_vout->output.i_chroma )
831 case VLC_FOURCC('I','4','2','0'):
833 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
834 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
836 /* Allocate the memory buffer */
837 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
838 16, i_width * i_height * 3 / 2 );
841 p_pic->Y_PIXELS = p_pic->p_data;
842 p_pic->p[Y_PLANE].i_lines = i_height;
843 p_pic->p[Y_PLANE].i_pitch = i_width;
844 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
845 p_pic->p[Y_PLANE].i_visible_pitch = i_width;
848 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
849 p_pic->p[U_PLANE].i_lines = i_height / 2;
850 p_pic->p[U_PLANE].i_pitch = i_width / 2;
851 p_pic->p[U_PLANE].i_pixel_pitch = 1;
852 p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
855 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
856 p_pic->p[V_PLANE].i_lines = i_height / 2;
857 p_pic->p[V_PLANE].i_pitch = i_width / 2;
858 p_pic->p[V_PLANE].i_pixel_pitch = 1;
859 p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
861 /* We allocated 3 planes */
864 #define P p_pic->p_sys->pixmap_i420
865 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
866 - p_pic->p_sys->p_info;
867 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
868 - p_pic->p_sys->p_info;
869 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
870 - p_pic->p_sys->p_info;
872 P.componentInfoY.rowBytes = i_width;
873 P.componentInfoCb.rowBytes = i_width / 2;
874 P.componentInfoCr.rowBytes = i_width / 2;
880 /* Unknown chroma, tell the guy to get lost */
881 free( p_pic->p_sys );
882 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
883 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
891 /*****************************************************************************
892 * QTFreePicture: destroy a picture allocated with QTNewPicture
893 *****************************************************************************/
894 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
896 switch( p_vout->output.i_chroma )
898 case VLC_FOURCC('I','4','2','0'):
899 free( p_pic->p_data_orig );
903 free( p_pic->p_sys );
906 /*****************************************************************************
907 * VLCWindow implementation
908 *****************************************************************************/
909 @implementation VLCWindow
911 - (void)setVout:(vout_thread_t *)_p_vout
916 - (vout_thread_t *)getVout
921 - (void)scaleWindowWithFactor: (float)factor
924 int i_corrected_height, i_corrected_width;
926 NSPoint topleftscreen;
928 if ( !p_vout->b_fullscreen )
931 topleftbase.y = [self frame].size.height;
932 topleftscreen = [self convertBaseToScreen: topleftbase];
934 if( p_vout->output.i_height * p_vout->output.i_aspect >
935 p_vout->output.i_width * VOUT_ASPECT_FACTOR )
937 i_corrected_width = p_vout->output.i_height * p_vout->output.i_aspect /
939 newsize.width = (int) ( i_corrected_width * factor );
940 newsize.height = (int) ( p_vout->render.i_height * factor );
944 i_corrected_height = p_vout->output.i_width * VOUT_ASPECT_FACTOR /
945 p_vout->output.i_aspect;
946 newsize.width = (int) ( p_vout->render.i_width * factor );
947 newsize.height = (int) ( i_corrected_height * factor );
950 [self setContentSize: newsize];
952 [self setFrameTopLeftPoint: topleftscreen];
953 p_vout->i_changes |= VOUT_SIZE_CHANGE;
957 - (void)toggleFloatOnTop
959 if( config_GetInt( p_vout, "macosx-float" ) )
961 config_PutInt( p_vout, "macosx-float", 0 );
962 [p_vout->p_sys->o_window setLevel: NSNormalWindowLevel];
966 config_PutInt( p_vout, "macosx-float", 1 );
967 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
971 - (void)toggleFullscreen
973 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
978 return( p_vout->b_fullscreen );
981 - (BOOL)canBecomeKeyWindow
986 - (void)keyDown:(NSEvent *)o_event
991 if( [[o_event characters] length] )
993 key = [[o_event characters] characterAtIndex: 0];
999 [self toggleFullscreen];
1002 case (unichar)0x1b: /* escape */
1003 if( [self isFullscreen] )
1005 [self toggleFullscreen];
1010 p_vout->p_vlc->b_die = VLC_TRUE;
1014 input_SetStatus( p_vout, INPUT_STATUS_PAUSE );
1017 case (unichar)0xf700: /* arrow up */
1018 val.psz_string = "UP";
1019 var_Set( p_vout, "key-pressed", val );
1022 case (unichar)0xf701: /* arrow down */
1023 val.psz_string = "DOWN";
1024 var_Set( p_vout, "key-pressed", val );
1027 case (unichar)0xf702: /* arrow left */
1028 val.psz_string = "LEFT";
1029 var_Set( p_vout, "key-pressed", val );
1032 case (unichar)0xf703: /* arrow right */
1033 val.psz_string = "RIGHT";
1034 var_Set( p_vout, "key-pressed", val );
1037 case (unichar)0xd: /* return */
1038 case (unichar)0x3: /* enter */
1039 val.psz_string = "ENTER";
1040 var_Set( p_vout, "key-pressed", val );
1045 [super keyDown: o_event];
1052 NSMutableString * o_title;
1053 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1056 if( p_playlist == NULL )
1061 vlc_mutex_lock( &p_playlist->object_lock );
1062 o_title = [NSMutableString stringWithUTF8String:
1063 p_playlist->pp_items[p_playlist->i_index]->psz_name];
1064 vlc_mutex_unlock( &p_playlist->object_lock );
1066 vlc_object_release( p_playlist );
1068 if( o_title != nil )
1070 NSRange prefix_range = [o_title rangeOfString: @"file:"];
1071 if( prefix_range.location != NSNotFound )
1073 [o_title deleteCharactersInRange: prefix_range];
1076 [self setTitleWithRepresentedFilename: o_title];
1081 [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
1085 /* This is actually the same as VLCControls::stop. */
1086 - (BOOL)windowShouldClose:(id)sender
1088 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1090 if( p_playlist == NULL )
1095 playlist_Stop( p_playlist );
1096 vlc_object_release( p_playlist );
1098 /* The window will be closed by the intf later. */
1104 /*****************************************************************************
1105 * VLCView implementation
1106 *****************************************************************************/
1107 @implementation VLCView
1109 - (void)drawRect:(NSRect)rect
1111 vout_thread_t * p_vout;
1112 id o_window = [self window];
1113 p_vout = (vout_thread_t *)[o_window getVout];
1115 [[NSColor blackColor] set];
1117 [super drawRect: rect];
1119 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1122 - (BOOL)acceptsFirstResponder
1127 - (BOOL)becomeFirstResponder
1129 vout_thread_t * p_vout;
1130 id o_window = [self window];
1131 p_vout = (vout_thread_t *)[o_window getVout];
1133 [o_window setAcceptsMouseMovedEvents: YES];
1137 - (BOOL)resignFirstResponder
1139 vout_thread_t * p_vout;
1140 id o_window = [self window];
1141 p_vout = (vout_thread_t *)[o_window getVout];
1143 [o_window setAcceptsMouseMovedEvents: NO];
1144 VLCHideMouse( p_vout, NO );
1148 - (void)mouseDown:(NSEvent *)o_event
1150 vout_thread_t * p_vout;
1151 id o_window = [self window];
1152 p_vout = (vout_thread_t *)[o_window getVout];
1155 switch( [o_event type] )
1157 case NSLeftMouseDown:
1159 var_Get( p_vout, "mouse-button-down", &val );
1161 var_Set( p_vout, "mouse-button-down", val );
1166 [super mouseDown: o_event];
1171 - (void)otherMouseDown:(NSEvent *)o_event
1173 /* This is not the the wheel button. you need to poll the
1174 * mouseWheel event for that. other is a third, forth or fifth button */
1175 vout_thread_t * p_vout;
1176 id o_window = [self window];
1177 p_vout = (vout_thread_t *)[o_window getVout];
1180 switch( [o_event type] )
1182 case NSOtherMouseDown:
1184 var_Get( p_vout, "mouse-button-down", &val );
1186 var_Set( p_vout, "mouse-button-down", val );
1191 [super mouseDown: o_event];
1196 - (void)rightMouseDown:(NSEvent *)o_event
1198 vout_thread_t * p_vout;
1199 id o_window = [self window];
1200 p_vout = (vout_thread_t *)[o_window getVout];
1203 switch( [o_event type] )
1205 case NSRightMouseDown:
1207 var_Get( p_vout, "mouse-button-down", &val );
1209 var_Set( p_vout, "mouse-button-down", val );
1214 [super mouseDown: o_event];
1219 - (void)mouseUp:(NSEvent *)o_event
1221 vout_thread_t * p_vout;
1222 id o_window = [self window];
1223 p_vout = (vout_thread_t *)[o_window getVout];
1226 switch( [o_event type] )
1231 b_val.b_bool = VLC_TRUE;
1232 var_Set( p_vout, "mouse-clicked", b_val );
1234 var_Get( p_vout, "mouse-button-down", &val );
1236 var_Set( p_vout, "mouse-button-down", val );
1241 [super mouseUp: o_event];
1246 - (void)otherMouseUp:(NSEvent *)o_event
1248 vout_thread_t * p_vout;
1249 id o_window = [self window];
1250 p_vout = (vout_thread_t *)[o_window getVout];
1253 switch( [o_event type] )
1255 case NSOtherMouseUp:
1257 var_Get( p_vout, "mouse-button-down", &val );
1259 var_Set( p_vout, "mouse-button-down", val );
1264 [super mouseUp: o_event];
1269 - (void)rightMouseUp:(NSEvent *)o_event
1271 vout_thread_t * p_vout;
1272 id o_window = [self window];
1273 p_vout = (vout_thread_t *)[o_window getVout];
1276 switch( [o_event type] )
1278 case NSRightMouseUp:
1280 var_Get( p_vout, "mouse-button-down", &val );
1282 var_Set( p_vout, "mouse-button-down", val );
1287 [super mouseUp: o_event];
1292 - (void)mouseDragged:(NSEvent *)o_event
1294 [self mouseMoved:o_event];
1297 - (void)otherMouseDragged:(NSEvent *)o_event
1299 [self mouseMoved:o_event];
1302 - (void)rightMouseDragged:(NSEvent *)o_event
1304 [self mouseMoved:o_event];
1307 - (void)mouseMoved:(NSEvent *)o_event
1313 vout_thread_t * p_vout;
1314 id o_window = [self window];
1315 p_vout = (vout_thread_t *)[o_window getVout];
1317 s_rect = [self bounds];
1318 ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1319 b_inside = [self mouse: ml inRect: s_rect];
1324 int i_width, i_height, i_x, i_y;
1326 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1327 (unsigned int)s_rect.size.height,
1328 &i_x, &i_y, &i_width, &i_height );
1330 val.i_int = ( ((int)ml.x) - i_x ) *
1331 p_vout->render.i_width / i_width;
1332 var_Set( p_vout, "mouse-x", val );
1334 val.i_int = ( ((int)ml.y) - i_y ) *
1335 p_vout->render.i_height / i_height;
1336 var_Set( p_vout, "mouse-y", val );
1338 val.b_bool = VLC_TRUE;
1339 var_Set( p_vout, "mouse-moved", val );
1340 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1341 p_vout->p_sys->b_mouse_moved = YES;
1343 else if ( !b_inside && !p_vout->p_sys->b_mouse_pointer_visible )
1345 /* people with multiple monitors need their mouse,
1346 * even if VLCView in fullscreen. */
1347 VLCHideMouse( p_vout, NO );
1350 [super mouseMoved: o_event];
1355 /*****************************************************************************
1356 * VLCVout implementation
1357 *****************************************************************************/
1358 @implementation VLCVout
1360 - (void)createWindow:(NSValue *)o_value
1364 NSScreen * o_screen;
1365 vout_thread_t * p_vout;
1366 vlc_bool_t b_main_screen;
1368 p_vout = (vout_thread_t *)[o_value pointerValue];
1370 p_vout->p_sys->o_window = [VLCWindow alloc];
1371 [p_vout->p_sys->o_window setVout: p_vout];
1372 [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1374 if( var_Get( p_vout, "video-device", &val ) < 0 )
1376 o_screen = [NSScreen mainScreen];
1381 NSArray *o_screens = [NSScreen screens];
1382 unsigned int i_index = val.i_int;
1384 if( [o_screens count] < i_index )
1386 o_screen = [NSScreen mainScreen];
1392 o_screen = [o_screens objectAtIndex: i_index];
1393 config_PutInt( p_vout, "macosx-vdev", i_index );
1394 b_main_screen = (i_index == 0);
1398 if( p_vout->b_fullscreen )
1400 NSRect screen_rect = [o_screen frame];
1401 screen_rect.origin.x = screen_rect.origin.y = 0;
1403 if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1404 BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1405 NULL, NULL, fullScreenAllowEvents );
1407 [p_vout->p_sys->o_window
1408 initWithContentRect: screen_rect
1409 styleMask: NSBorderlessWindowMask
1410 backing: NSBackingStoreBuffered
1411 defer: NO screen: o_screen];
1413 [p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1414 p_vout->p_sys->b_mouse_moved = YES;
1415 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1419 unsigned int i_stylemask = NSTitledWindowMask |
1420 NSMiniaturizableWindowMask |
1421 NSClosableWindowMask |
1422 NSResizableWindowMask;
1424 if ( p_vout->p_sys->p_fullscreen_state != NULL )
1425 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1426 p_vout->p_sys->p_fullscreen_state = NULL;
1428 [p_vout->p_sys->o_window
1429 initWithContentRect: p_vout->p_sys->s_rect
1430 styleMask: i_stylemask
1431 backing: NSBackingStoreBuffered
1432 defer: NO screen: o_screen];
1434 [p_vout->p_sys->o_window setAlphaValue: config_GetFloat( p_vout, "macosx-opaqueness" )];
1436 if( config_GetInt( p_vout, "macosx-float" ) )
1438 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
1441 if( !p_vout->p_sys->b_pos_saved )
1443 [p_vout->p_sys->o_window center];
1447 o_view = [[VLCView alloc] init];
1449 /* FIXME: [o_view setMenu:] */
1450 [p_vout->p_sys->o_window setContentView: o_view];
1451 [o_view autorelease];
1454 p_vout->p_sys->p_qdport = [o_view qdPort];
1456 [o_view unlockFocus];
1458 [p_vout->p_sys->o_window updateTitle];
1459 [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1462 - (void)destroyWindow:(NSValue *)o_value
1464 vout_thread_t * p_vout;
1466 p_vout = (vout_thread_t *)[o_value pointerValue];
1468 if( !p_vout->b_fullscreen )
1472 s_rect = [[p_vout->p_sys->o_window contentView] frame];
1473 p_vout->p_sys->s_rect.size = s_rect.size;
1475 s_rect = [p_vout->p_sys->o_window frame];
1476 p_vout->p_sys->s_rect.origin = s_rect.origin;
1478 p_vout->p_sys->b_pos_saved = YES;
1481 p_vout->p_sys->p_qdport = nil;
1482 [p_vout->p_sys->o_window close];
1483 p_vout->p_sys->o_window = nil;