1 /*****************************************************************************
2 * vout.m: MacOS X video output module
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: vout.m,v 1.76 2004/02/02 08:50:41 titer Exp $
7 * Authors: Colin Delacroix <colin@zoy.org>
8 * Florian G. Pflug <fgp@phlo.org>
9 * Jon Lech Johansen <jon-vl@nanocrew.net>
10 * Derk-Jan Hartman <thedj@users.sourceforge.net>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <errno.h> /* ENOMEM */
31 #include <stdlib.h> /* free() */
32 #include <string.h> /* strerror() */
34 #include <QuickTime/QuickTime.h>
36 #include <OpenGL/OpenGL.h>
37 #include <OpenGL/gl.h>
38 #include <OpenGL/glext.h>
45 #define QT_MAX_DIRECTBUFFERS 10
46 #define VL_MAX_DISPLAYS 16
53 /* When using I420 output */
54 PlanarPixmapInfoYUV420 pixmap_i420;
57 /*****************************************************************************
59 *****************************************************************************/
61 static int vout_Init ( vout_thread_t * );
62 static void vout_End ( vout_thread_t * );
63 static int vout_Manage ( vout_thread_t * );
64 static void vout_Display ( vout_thread_t *, picture_t * );
66 static int CoSendRequest ( vout_thread_t *, SEL );
67 static int CoCreateWindow ( vout_thread_t * );
68 static int CoDestroyWindow ( vout_thread_t * );
69 static int CoToggleFullscreen ( vout_thread_t * );
71 static void VLCHideMouse ( vout_thread_t *, BOOL );
73 static void QTScaleMatrix ( vout_thread_t * );
74 static int QTCreateSequence ( vout_thread_t * );
75 static void QTDestroySequence ( vout_thread_t * );
76 static int QTNewPicture ( vout_thread_t *, picture_t * );
77 static void QTFreePicture ( vout_thread_t *, picture_t * );
79 /*****************************************************************************
80 * OpenVideo: allocates MacOS X video thread output method
81 *****************************************************************************
82 * This function allocates and initializes a MacOS X vout method.
83 *****************************************************************************/
84 int E_(OpenVideo) ( vlc_object_t *p_this )
86 vout_thread_t * p_vout = (vout_thread_t *)p_this;
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 /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
100 for( i_timeout = 20 ; i_timeout-- ; )
104 msleep( INTF_IDLE_SLEEP );
110 /* no MacOS X intf, unable to communicate with MT */
111 msg_Err( p_vout, "no MacOS X interface present" );
112 free( p_vout->p_sys );
116 if( [NSApp respondsToSelector: @selector(getIntf)] )
118 intf_thread_t * p_intf;
120 for( i_timeout = 10 ; i_timeout-- ; )
122 if( ( p_intf = [NSApp getIntf] ) == NULL )
124 msleep( INTF_IDLE_SLEEP );
130 msg_Err( p_vout, "MacOS X intf has getIntf, but is NULL" );
131 free( p_vout->p_sys );
136 p_vout->p_sys->b_mouse_moved = VLC_TRUE;
137 p_vout->p_sys->i_time_mouse_last_moved = mdate();
139 /* set window size */
140 p_vout->p_sys->s_rect.size.width = p_vout->i_window_width;
141 p_vout->p_sys->s_rect.size.height = p_vout->i_window_height;
143 /* Check if we should use QuickTime or OpenGL */
144 p_vout->p_sys->i_opengl = config_GetInt( p_vout, "macosx-opengl" );
146 if( !p_vout->p_sys->i_opengl )
148 /* Initialize QuickTime */
149 p_vout->p_sys->h_img_descr =
150 (ImageDescriptionHandle)NewHandleClear( sizeof(ImageDescription) );
151 p_vout->p_sys->p_matrix =
152 (MatrixRecordPtr)malloc( sizeof(MatrixRecord) );
153 p_vout->p_sys->p_fullscreen_state = NULL;
155 if( ( err = EnterMovies() ) != noErr )
157 msg_Err( p_vout, "EnterMovies failed: %d", err );
158 free( p_vout->p_sys->p_matrix );
159 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
160 free( p_vout->p_sys );
164 /* Damn QT isn't thread safe. so keep a lock in the p_vlc object */
165 vlc_mutex_lock( &p_vout->p_vlc->quicktime_lock );
167 err = FindCodec( kYUV420CodecType, bestSpeedCodec,
168 nil, &p_vout->p_sys->img_dc );
170 vlc_mutex_unlock( &p_vout->p_vlc->quicktime_lock );
171 if( err == noErr && p_vout->p_sys->img_dc != 0 )
173 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
174 p_vout->p_sys->i_codec = kYUV420CodecType;
178 msg_Err( p_vout, "failed to find an appropriate codec" );
181 if( p_vout->p_sys->img_dc == 0 )
183 free( p_vout->p_sys->p_matrix );
184 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
185 free( p_vout->p_sys );
190 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
191 NSArray * o_screens = [NSScreen screens];
192 if( [o_screens count] > 0 && var_Type( p_vout, "video-device" ) == 0 )
195 vlc_value_t val, text;
198 int i_option = config_GetInt( p_vout, "macosx-vdev" );
200 var_Create( p_vout, "video-device", VLC_VAR_INTEGER |
202 text.psz_string = _("Video device");
203 var_Change( p_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL );
205 NSEnumerator * o_enumerator = [o_screens objectEnumerator];
207 while( (o_screen = [o_enumerator nextObject]) != NULL )
210 NSRect s_rect = [o_screen frame];
212 snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
213 "%s %d (%dx%d)", _("Screen"), i,
214 (int)s_rect.size.width, (int)s_rect.size.height );
216 text.psz_string = psz_temp;
218 var_Change( p_vout, "video-device",
219 VLC_VAR_ADDCHOICE, &val, &text );
221 if( ( i - 1 ) == i_option )
223 var_Set( p_vout, "video-device", val );
228 var_AddCallback( p_vout, "video-device", vout_VarCallback,
231 val.b_bool = VLC_TRUE;
232 var_Set( p_vout, "intf-change", val );
236 if( CoCreateWindow( p_vout ) )
238 msg_Err( p_vout, "unable to create window" );
239 if( !p_vout->p_sys->i_opengl )
241 free( p_vout->p_sys->p_matrix );
242 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
244 free( p_vout->p_sys );
248 p_vout->pf_init = vout_Init;
249 p_vout->pf_end = vout_End;
250 p_vout->pf_manage = vout_Manage;
251 p_vout->pf_render = NULL;
252 p_vout->pf_display = vout_Display;
257 /*****************************************************************************
258 * vout_Init: initialize video thread output method
259 *****************************************************************************/
260 static int vout_Init( vout_thread_t *p_vout )
265 I_OUTPUTPICTURES = 0;
267 /* Initialize the output structure; we already found a codec,
268 * and the corresponding chroma we will be using. Since we can
269 * arbitrary scale, stick to the coordinates and aspect. */
270 p_vout->output.i_width = p_vout->render.i_width;
271 p_vout->output.i_height = p_vout->render.i_height;
272 p_vout->output.i_aspect = p_vout->render.i_aspect;
274 if( !p_vout->p_sys->i_opengl )
276 SetPort( p_vout->p_sys->p_qdport );
277 QTScaleMatrix( p_vout );
279 if( QTCreateSequence( p_vout ) )
281 msg_Err( p_vout, "unable to create sequence" );
287 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
288 p_vout->output.i_rmask = 0xFF0000;
289 p_vout->output.i_gmask = 0x00FF00;
290 p_vout->output.i_bmask = 0x0000FF;
293 /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
294 while( I_OUTPUTPICTURES < QT_MAX_DIRECTBUFFERS )
298 /* Find an empty picture slot */
299 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
301 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
303 p_pic = p_vout->p_picture + i_index;
308 /* Allocate the picture */
314 if( !p_vout->p_sys->i_opengl )
316 if( QTNewPicture( p_vout, p_pic ) )
323 /* Nothing special to do, we just need a basic allocated
325 vout_AllocatePicture( p_vout, p_pic, p_vout->output.i_chroma,
326 p_vout->output.i_width, p_vout->output.i_height,
327 p_vout->output.i_aspect );
330 p_pic->i_status = DESTROYED_PICTURE;
331 p_pic->i_type = DIRECT_PICTURE;
333 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
338 if( p_vout->p_sys->i_opengl )
340 [p_vout->p_sys->o_glview lockFocus];
341 [p_vout->p_sys->o_glview initTextures];
342 [p_vout->p_sys->o_glview unlockFocus];
348 /*****************************************************************************
349 * vout_End: terminate video thread output method
350 *****************************************************************************/
351 static void vout_End( vout_thread_t *p_vout )
355 if( !p_vout->p_sys->i_opengl )
357 QTDestroySequence( p_vout );
360 /* Free the direct buffers we allocated */
361 for( i_index = I_OUTPUTPICTURES; i_index; )
364 if( !p_vout->p_sys->i_opengl )
366 QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
370 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
375 /*****************************************************************************
376 * CloseVideo: destroy video thread output method
377 *****************************************************************************/
378 void E_(CloseVideo) ( vlc_object_t *p_this )
380 vout_thread_t * p_vout = (vout_thread_t *)p_this;
382 if( CoDestroyWindow( p_vout ) )
384 msg_Err( p_vout, "unable to destroy window" );
387 if( !p_vout->p_sys->i_opengl )
389 if ( p_vout->p_sys->p_fullscreen_state != NULL )
390 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
394 free( p_vout->p_sys->p_matrix );
395 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
398 free( p_vout->p_sys );
401 /*****************************************************************************
402 * vout_Manage: handle events
403 *****************************************************************************
404 * This function should be called regularly by video output thread. It manages
405 * console events. It returns a non null value on error.
406 *****************************************************************************/
407 static int vout_Manage( vout_thread_t *p_vout )
409 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
411 if( CoToggleFullscreen( p_vout ) )
416 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
419 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
421 if( !p_vout->p_sys->i_opengl )
423 QTScaleMatrix( p_vout );
424 SetDSequenceMatrix( p_vout->p_sys->i_seq,
425 p_vout->p_sys->p_matrix );
428 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
431 /* hide/show mouse cursor
432 * this code looks unnecessarily complicated, but is necessary like this.
433 * it has to deal with multiple monitors and therefore checks a lot */
434 if( !p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
436 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 3000000 )
438 VLCHideMouse( p_vout, YES );
441 else if ( p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
443 VLCHideMouse( p_vout, NO );
446 /* disable screen saver */
447 UpdateSystemActivity( UsrActivity );
452 /*****************************************************************************
453 * vout_Display: displays previously rendered output
454 *****************************************************************************
455 * This function sends the currently rendered image to the display.
456 *****************************************************************************/
457 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
459 if( !p_vout->p_sys->i_opengl )
464 if( ( err = DecompressSequenceFrameS(
465 p_vout->p_sys->i_seq,
466 p_pic->p_sys->p_info,
467 p_pic->p_sys->i_size,
468 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
470 msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
474 QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
479 if( [p_vout->p_sys->o_glview lockFocusIfCanDraw] )
481 [p_vout->p_sys->o_glview reloadTexture: p_pic];
482 [p_vout->p_sys->o_glview drawRect:
483 [p_vout->p_sys->o_glview bounds]];
484 [p_vout->p_sys->o_glview unlockFocus];
489 /*****************************************************************************
490 * CoSendRequest: send request to interface thread
491 *****************************************************************************
492 * Returns 0 on success, 1 otherwise
493 *****************************************************************************/
494 static int CoSendRequest( vout_thread_t *p_vout, SEL sel )
498 intf_thread_t * p_intf;
500 VLCVout * o_vlv = [[VLCVout alloc] init];
502 if( ( i_ret = ExecuteOnMainThread( o_vlv, sel, (void *)p_vout ) ) )
504 msg_Err( p_vout, "SendRequest: no way to communicate with mt" );
509 /*This makes this function dependant of the presence of a macosx
510 interface. We do not check if this interface exists, since it has
511 already been done before.*/
513 p_intf = [NSApp getIntf];
515 val.b_bool = VLC_TRUE;
516 var_Create(p_intf,"intf-change",VLC_VAR_BOOL);
517 var_Set(p_intf, "intf-change",val);
522 /*****************************************************************************
523 * CoCreateWindow: create new window
524 *****************************************************************************
525 * Returns 0 on success, 1 otherwise
526 *****************************************************************************/
527 static int CoCreateWindow( vout_thread_t *p_vout )
529 if( CoSendRequest( p_vout, @selector(createWindow:) ) )
531 msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
538 /*****************************************************************************
539 * CoDestroyWindow: destroy window
540 *****************************************************************************
541 * Returns 0 on success, 1 otherwise
542 *****************************************************************************/
543 static int CoDestroyWindow( vout_thread_t *p_vout )
546 VLCHideMouse( p_vout, NO );
548 if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
550 msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
557 /*****************************************************************************
558 * CoToggleFullscreen: toggle fullscreen
559 *****************************************************************************
560 * Returns 0 on success, 1 otherwise
561 *****************************************************************************/
562 static int CoToggleFullscreen( vout_thread_t *p_vout )
564 if( p_vout->p_sys->i_opengl )
570 QTDestroySequence( p_vout );
572 if( CoDestroyWindow( p_vout ) )
574 msg_Err( p_vout, "unable to destroy window" );
578 p_vout->b_fullscreen = !p_vout->b_fullscreen;
580 if( CoCreateWindow( p_vout ) )
582 msg_Err( p_vout, "unable to create window" );
586 SetPort( p_vout->p_sys->p_qdport );
587 QTScaleMatrix( p_vout );
589 if( QTCreateSequence( p_vout ) )
591 msg_Err( p_vout, "unable to create sequence" );
598 /*****************************************************************************
599 * VLCHideMouse: if b_hide then hide the cursor
600 *****************************************************************************/
601 static void VLCHideMouse ( vout_thread_t *p_vout, BOOL b_hide )
606 NSWindow *o_window = p_vout->p_sys->o_window;
607 NSView *o_contents = [o_window contentView];
609 s_rect = [o_contents bounds];
610 ml = [o_window convertScreenToBase:[NSEvent mouseLocation]];
611 ml = [o_contents convertPoint:ml fromView:nil];
612 b_inside = [o_contents mouse: ml inRect: s_rect];
614 if ( b_hide && b_inside )
616 /* only hide if mouse over VLCQTView */
617 [NSCursor setHiddenUntilMouseMoves: YES];
621 [NSCursor setHiddenUntilMouseMoves: NO];
623 p_vout->p_sys->b_mouse_moved = NO;
624 p_vout->p_sys->i_time_mouse_last_moved = mdate();
628 /*****************************************************************************
629 * QTScaleMatrix: scale matrix
630 *****************************************************************************/
631 static void QTScaleMatrix( vout_thread_t *p_vout )
634 unsigned int i_width, i_height;
635 Fixed factor_x, factor_y;
636 unsigned int i_offset_x = 0;
637 unsigned int i_offset_y = 0;
639 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
641 i_width = s_rect.right - s_rect.left;
642 i_height = s_rect.bottom - s_rect.top;
644 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
646 int i_adj_width = i_height * p_vout->output.i_aspect /
649 factor_x = FixDiv( Long2Fix( i_adj_width ),
650 Long2Fix( p_vout->output.i_width ) );
651 factor_y = FixDiv( Long2Fix( i_height ),
652 Long2Fix( p_vout->output.i_height ) );
654 i_offset_x = (i_width - i_adj_width) / 2;
658 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
659 p_vout->output.i_aspect;
661 factor_x = FixDiv( Long2Fix( i_width ),
662 Long2Fix( p_vout->output.i_width ) );
663 factor_y = FixDiv( Long2Fix( i_adj_height ),
664 Long2Fix( p_vout->output.i_height ) );
666 i_offset_y = (i_height - i_adj_height) / 2;
669 SetIdentityMatrix( p_vout->p_sys->p_matrix );
671 ScaleMatrix( p_vout->p_sys->p_matrix,
673 Long2Fix(0), Long2Fix(0) );
675 TranslateMatrix( p_vout->p_sys->p_matrix,
676 Long2Fix(i_offset_x),
677 Long2Fix(i_offset_y) );
681 /*****************************************************************************
682 * QTCreateSequence: create a new sequence
683 *****************************************************************************
684 * Returns 0 on success, 1 otherwise
685 *****************************************************************************/
686 static int QTCreateSequence( vout_thread_t *p_vout )
689 ImageDescriptionPtr p_descr;
691 HLock( (Handle)p_vout->p_sys->h_img_descr );
692 p_descr = *p_vout->p_sys->h_img_descr;
694 p_descr->idSize = sizeof(ImageDescription);
695 p_descr->cType = p_vout->p_sys->i_codec;
696 p_descr->version = 1;
697 p_descr->revisionLevel = 0;
698 p_descr->vendor = 'appl';
699 p_descr->width = p_vout->output.i_width;
700 p_descr->height = p_vout->output.i_height;
701 p_descr->hRes = Long2Fix(72);
702 p_descr->vRes = Long2Fix(72);
703 p_descr->spatialQuality = codecLosslessQuality;
704 p_descr->frameCount = 1;
705 p_descr->clutID = -1;
706 p_descr->dataSize = 0;
709 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
711 if( ( err = DecompressSequenceBeginS(
712 &p_vout->p_sys->i_seq,
713 p_vout->p_sys->h_img_descr,
715 p_vout->p_sys->p_qdport,
717 p_vout->p_sys->p_matrix,
719 codecFlagUseImageBuffer,
720 codecLosslessQuality,
721 p_vout->p_sys->img_dc ) ) )
723 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
730 /*****************************************************************************
731 * QTDestroySequence: destroy sequence
732 *****************************************************************************/
733 static void QTDestroySequence( vout_thread_t *p_vout )
735 CDSequenceEnd( p_vout->p_sys->i_seq );
738 /*****************************************************************************
739 * QTNewPicture: allocate a picture
740 *****************************************************************************
741 * Returns 0 on success, 1 otherwise
742 *****************************************************************************/
743 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
745 int i_width = p_vout->output.i_width;
746 int i_height = p_vout->output.i_height;
748 /* We know the chroma, allocate a buffer which will be used
749 * directly by the decoder */
750 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
752 if( p_pic->p_sys == NULL )
757 switch( p_vout->output.i_chroma )
759 case VLC_FOURCC('I','4','2','0'):
761 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
762 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
764 /* Allocate the memory buffer */
765 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
766 16, i_width * i_height * 3 / 2 );
769 p_pic->Y_PIXELS = p_pic->p_data;
770 p_pic->p[Y_PLANE].i_lines = i_height;
771 p_pic->p[Y_PLANE].i_pitch = i_width;
772 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
773 p_pic->p[Y_PLANE].i_visible_pitch = i_width;
776 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
777 p_pic->p[U_PLANE].i_lines = i_height / 2;
778 p_pic->p[U_PLANE].i_pitch = i_width / 2;
779 p_pic->p[U_PLANE].i_pixel_pitch = 1;
780 p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
783 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
784 p_pic->p[V_PLANE].i_lines = i_height / 2;
785 p_pic->p[V_PLANE].i_pitch = i_width / 2;
786 p_pic->p[V_PLANE].i_pixel_pitch = 1;
787 p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
789 /* We allocated 3 planes */
792 #define P p_pic->p_sys->pixmap_i420
793 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
794 - p_pic->p_sys->p_info;
795 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
796 - p_pic->p_sys->p_info;
797 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
798 - p_pic->p_sys->p_info;
800 P.componentInfoY.rowBytes = i_width;
801 P.componentInfoCb.rowBytes = i_width / 2;
802 P.componentInfoCr.rowBytes = i_width / 2;
808 /* Unknown chroma, tell the guy to get lost */
809 free( p_pic->p_sys );
810 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
811 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
819 /*****************************************************************************
820 * QTFreePicture: destroy a picture allocated with QTNewPicture
821 *****************************************************************************/
822 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
824 switch( p_vout->output.i_chroma )
826 case VLC_FOURCC('I','4','2','0'):
827 free( p_pic->p_data_orig );
831 free( p_pic->p_sys );
834 /*****************************************************************************
835 * VLCWindow implementation
836 *****************************************************************************/
837 @implementation VLCWindow
839 - (void)setVout:(vout_thread_t *)_p_vout
844 - (vout_thread_t *)getVout
849 - (void)scaleWindowWithFactor: (float)factor
852 int i_corrected_height, i_corrected_width;
854 NSPoint topleftscreen;
856 if ( !p_vout->b_fullscreen )
859 topleftbase.y = [self frame].size.height;
860 topleftscreen = [self convertBaseToScreen: topleftbase];
862 if( p_vout->output.i_height * p_vout->output.i_aspect >
863 p_vout->output.i_width * VOUT_ASPECT_FACTOR )
865 i_corrected_width = p_vout->output.i_height * p_vout->output.i_aspect /
867 newsize.width = (int) ( i_corrected_width * factor );
868 newsize.height = (int) ( p_vout->render.i_height * factor );
872 i_corrected_height = p_vout->output.i_width * VOUT_ASPECT_FACTOR /
873 p_vout->output.i_aspect;
874 newsize.width = (int) ( p_vout->render.i_width * factor );
875 newsize.height = (int) ( i_corrected_height * factor );
878 [self setContentSize: newsize];
880 [self setFrameTopLeftPoint: topleftscreen];
881 p_vout->i_changes |= VOUT_SIZE_CHANGE;
885 - (void)toggleFloatOnTop
887 if( config_GetInt( p_vout, "video-on-top" ) )
889 config_PutInt( p_vout, "video-on-top", 0 );
890 [p_vout->p_sys->o_window setLevel: NSNormalWindowLevel];
894 config_PutInt( p_vout, "video-on-top", 1 );
895 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
899 - (void)toggleFullscreen
901 config_PutInt(p_vout, "fullscreen", !p_vout->b_fullscreen);
902 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
907 return( p_vout->b_fullscreen );
910 - (BOOL)canBecomeKeyWindow
915 - (void)keyDown:(NSEvent *)o_event
919 unsigned int i_pressed_modifiers = 0;
922 i_pressed_modifiers = [o_event modifierFlags];
924 if( i_pressed_modifiers & NSShiftKeyMask )
925 val.i_int |= KEY_MODIFIER_SHIFT;
926 if( i_pressed_modifiers & NSControlKeyMask )
927 val.i_int |= KEY_MODIFIER_CTRL;
928 if( i_pressed_modifiers & NSAlternateKeyMask )
929 val.i_int |= KEY_MODIFIER_ALT;
930 if( i_pressed_modifiers & NSCommandKeyMask )
931 val.i_int |= KEY_MODIFIER_COMMAND;
933 key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
937 /* Escape should always get you out of fullscreen */
938 if( key == (unichar) 0x1b )
940 if( [self isFullscreen] )
942 [self toggleFullscreen];
945 else if ( key == ' ' )
947 playlist_t *p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
949 if ( p_playlist != NULL )
951 playlist_Pause( p_playlist );
952 vlc_object_release( p_playlist);
957 val.i_int |= CocoaKeyToVLC( key );
958 var_Set( p_vout->p_vlc, "key-pressed", val );
963 [super keyDown: o_event];
969 NSMutableString * o_title;
970 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
973 if( p_playlist == NULL )
978 vlc_mutex_lock( &p_playlist->object_lock );
979 o_title = [NSMutableString stringWithUTF8String:
980 p_playlist->pp_items[p_playlist->i_index]->psz_uri];
981 vlc_mutex_unlock( &p_playlist->object_lock );
983 vlc_object_release( p_playlist );
987 NSRange prefix_range = [o_title rangeOfString: @"file:"];
988 if( prefix_range.location != NSNotFound )
990 [o_title deleteCharactersInRange: prefix_range];
993 [self setTitleWithRepresentedFilename: o_title];
998 [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
1002 /* This is actually the same as VLCControls::stop. */
1003 - (BOOL)windowShouldClose:(id)sender
1005 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1007 if( p_playlist == NULL )
1012 playlist_Stop( p_playlist );
1013 vlc_object_release( p_playlist );
1015 /* The window will be closed by the intf later. */
1021 /*****************************************************************************
1022 * VLCQTView implementation
1023 *****************************************************************************/
1024 @implementation VLCQTView
1026 - (void)drawRect:(NSRect)rect
1028 vout_thread_t * p_vout;
1029 id o_window = [self window];
1030 p_vout = (vout_thread_t *)[o_window getVout];
1032 [[NSColor blackColor] set];
1034 [super drawRect: rect];
1036 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1039 - (BOOL)acceptsFirstResponder
1044 - (BOOL)becomeFirstResponder
1046 vout_thread_t * p_vout;
1047 id o_window = [self window];
1048 p_vout = (vout_thread_t *)[o_window getVout];
1050 [o_window setAcceptsMouseMovedEvents: YES];
1054 - (BOOL)resignFirstResponder
1056 vout_thread_t * p_vout;
1057 id o_window = [self window];
1058 p_vout = (vout_thread_t *)[o_window getVout];
1060 [o_window setAcceptsMouseMovedEvents: NO];
1061 VLCHideMouse( p_vout, NO );
1065 - (void)mouseDown:(NSEvent *)o_event
1067 vout_thread_t * p_vout;
1068 id o_window = [self window];
1069 p_vout = (vout_thread_t *)[o_window getVout];
1072 switch( [o_event type] )
1074 case NSLeftMouseDown:
1076 var_Get( p_vout, "mouse-button-down", &val );
1078 var_Set( p_vout, "mouse-button-down", val );
1083 [super mouseDown: o_event];
1088 - (void)otherMouseDown:(NSEvent *)o_event
1090 /* This is not the the wheel button. you need to poll the
1091 * mouseWheel event for that. other is a third, forth or fifth button */
1092 vout_thread_t * p_vout;
1093 id o_window = [self window];
1094 p_vout = (vout_thread_t *)[o_window getVout];
1097 switch( [o_event type] )
1099 case NSOtherMouseDown:
1101 var_Get( p_vout, "mouse-button-down", &val );
1103 var_Set( p_vout, "mouse-button-down", val );
1108 [super mouseDown: o_event];
1113 - (void)rightMouseDown:(NSEvent *)o_event
1115 vout_thread_t * p_vout;
1116 id o_window = [self window];
1117 p_vout = (vout_thread_t *)[o_window getVout];
1120 switch( [o_event type] )
1122 case NSRightMouseDown:
1124 var_Get( p_vout, "mouse-button-down", &val );
1126 var_Set( p_vout, "mouse-button-down", val );
1131 [super mouseDown: o_event];
1136 - (void)mouseUp:(NSEvent *)o_event
1138 vout_thread_t * p_vout;
1139 id o_window = [self window];
1140 p_vout = (vout_thread_t *)[o_window getVout];
1143 switch( [o_event type] )
1148 b_val.b_bool = VLC_TRUE;
1149 var_Set( p_vout, "mouse-clicked", b_val );
1151 var_Get( p_vout, "mouse-button-down", &val );
1153 var_Set( p_vout, "mouse-button-down", val );
1158 [super mouseUp: o_event];
1163 - (void)otherMouseUp:(NSEvent *)o_event
1165 vout_thread_t * p_vout;
1166 id o_window = [self window];
1167 p_vout = (vout_thread_t *)[o_window getVout];
1170 switch( [o_event type] )
1172 case NSOtherMouseUp:
1174 var_Get( p_vout, "mouse-button-down", &val );
1176 var_Set( p_vout, "mouse-button-down", val );
1181 [super mouseUp: o_event];
1186 - (void)rightMouseUp:(NSEvent *)o_event
1188 vout_thread_t * p_vout;
1189 id o_window = [self window];
1190 p_vout = (vout_thread_t *)[o_window getVout];
1193 switch( [o_event type] )
1195 case NSRightMouseUp:
1197 var_Get( p_vout, "mouse-button-down", &val );
1199 var_Set( p_vout, "mouse-button-down", val );
1204 [super mouseUp: o_event];
1209 - (void)mouseDragged:(NSEvent *)o_event
1211 [self mouseMoved:o_event];
1214 - (void)otherMouseDragged:(NSEvent *)o_event
1216 [self mouseMoved:o_event];
1219 - (void)rightMouseDragged:(NSEvent *)o_event
1221 [self mouseMoved:o_event];
1224 - (void)mouseMoved:(NSEvent *)o_event
1230 vout_thread_t * p_vout;
1231 id o_window = [self window];
1232 p_vout = (vout_thread_t *)[o_window getVout];
1234 s_rect = [self bounds];
1235 ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1236 b_inside = [self mouse: ml inRect: s_rect];
1241 int i_width, i_height, i_x, i_y;
1243 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1244 (unsigned int)s_rect.size.height,
1245 &i_x, &i_y, &i_width, &i_height );
1247 val.i_int = ( ((int)ml.x) - i_x ) *
1248 p_vout->render.i_width / i_width;
1249 var_Set( p_vout, "mouse-x", val );
1251 val.i_int = ( ((int)ml.y) - i_y ) *
1252 p_vout->render.i_height / i_height;
1253 var_Set( p_vout, "mouse-y", val );
1255 val.b_bool = VLC_TRUE;
1256 var_Set( p_vout, "mouse-moved", val );
1257 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1258 p_vout->p_sys->b_mouse_moved = YES;
1261 [super mouseMoved: o_event];
1266 /*****************************************************************************
1267 * VLCGLView implementation
1268 *****************************************************************************/
1269 @implementation VLCGLView
1272 - (id) initWithFrame: (NSRect) frame vout: (vout_thread_t*) _p_vout
1276 NSOpenGLPixelFormatAttribute attribs[] =
1278 NSOpenGLPFAAccelerated,
1279 NSOpenGLPFANoRecovery,
1280 NSOpenGLPFADoubleBuffer,
1285 NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
1286 initWithAttributes: attribs];
1290 fprintf( stderr, "Cannot create NSOpenGLPixelFormat\n" );
1294 self = [super initWithFrame:frame pixelFormat: fmt];
1296 [[self openGLContext] makeCurrentContext];
1297 [[self openGLContext] update];
1299 glClearColor( 0.0, 0.0, 0.0, 0.0 );
1308 [[self openGLContext] makeCurrentContext];
1310 NSRect bounds = [self bounds];
1312 glViewport( 0, 0, (GLint) bounds.size.width,
1313 (GLint) bounds.size.height );
1315 /* Quad size is set in order to preserve the aspect ratio */
1316 if( bounds.size.height * p_vout->output.i_aspect <
1317 bounds.size.width * VOUT_ASPECT_FACTOR )
1319 f_x = bounds.size.height * p_vout->render.i_aspect /
1320 VOUT_ASPECT_FACTOR / bounds.size.width;
1326 f_y = bounds.size.width * VOUT_ASPECT_FACTOR /
1327 p_vout->render.i_aspect / bounds.size.height;
1331 - (void) initTextures
1334 GLuint pi_textures[QT_MAX_DIRECTBUFFERS];
1336 [[self openGLContext] makeCurrentContext];
1338 /* Create textures */
1339 glGenTextures( QT_MAX_DIRECTBUFFERS, pi_textures );
1341 for( i = 0; i < I_OUTPUTPICTURES; i++ )
1343 glEnable( GL_TEXTURE_RECTANGLE_EXT );
1344 glEnable( GL_UNPACK_CLIENT_STORAGE_APPLE );
1345 glEnable( GL_APPLE_texture_range );
1347 glBindTexture( GL_TEXTURE_RECTANGLE_EXT, pi_textures[i] );
1350 FIXME: the lines below are supposed to avoid a memcpy and get
1351 a noticeable performance boost, but they are annoying side
1352 effects at the moment -- titer
1354 /* Use AGP texturing */
1355 glTextureRangeAPPLE( GL_TEXTURE_RECTANGLE_EXT, 0, NULL );
1356 glTexParameteri( GL_TEXTURE_RECTANGLE_EXT,
1357 GL_TEXTURE_STORAGE_HINT_APPLE, GL_STORAGE_SHARED_APPLE );
1360 /* Tell the driver not to make a copy of the texture but to use
1362 glPixelStorei( GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE );
1364 /* Linear interpolation */
1365 glTexParameteri( GL_TEXTURE_RECTANGLE_EXT,
1366 GL_TEXTURE_MIN_FILTER, GL_LINEAR );
1367 glTexParameteri( GL_TEXTURE_RECTANGLE_EXT,
1368 GL_TEXTURE_MAG_FILTER, GL_LINEAR );
1370 /* I have no idea what this exactly does, but it seems to be
1371 necessary for scaling */
1372 glTexParameteri( GL_TEXTURE_RECTANGLE_EXT,
1373 GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE );
1374 glTexParameteri( GL_TEXTURE_RECTANGLE_EXT,
1375 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1376 glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
1378 glTexImage2D( GL_TEXTURE_RECTANGLE_EXT, 0, GL_RGBA,
1379 p_vout->output.i_width, p_vout->output.i_height, 0,
1380 GL_YCBCR_422_APPLE, GL_UNSIGNED_SHORT_8_8_APPLE,
1381 PP_OUTPUTPICTURE[i]->p_data );
1383 PP_OUTPUTPICTURE[i]->p_sys = malloc( sizeof( GLuint ) );
1384 *((GLuint*) PP_OUTPUTPICTURE[i]->p_sys) = pi_textures[i];
1390 - (void) reloadTexture: (picture_t *) p_pic
1392 i_cur_texture = *((GLuint*) p_pic->p_sys);
1394 [[self openGLContext] makeCurrentContext];
1396 glBindTexture( GL_TEXTURE_RECTANGLE_EXT, i_cur_texture );
1398 /* glTexSubImage2D is faster than glTexImage2D
1399 http://developer.apple.com/samplecode/Sample_Code/Graphics_3D/
1400 TextureRange/MainOpenGLView.m.htm */
1401 glTexSubImage2D( GL_TEXTURE_RECTANGLE_EXT, 0, 0, 0,
1402 p_vout->output.i_width, p_vout->output.i_height,
1403 GL_YCBCR_422_APPLE, GL_UNSIGNED_SHORT_8_8_APPLE,
1407 - (void) drawRect: (NSRect) rect
1409 [[self openGLContext] makeCurrentContext];
1411 /* Swap buffers only during the vertical retrace of the monitor.
1412 http://developer.apple.com/documentation/GraphicsImaging/
1413 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
1414 long params[] = { 1 };
1415 CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
1418 /* Black background */
1419 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
1423 [[self openGLContext] flushBuffer];
1427 /* Draw a quad with our texture on it */
1428 glBindTexture( GL_TEXTURE_RECTANGLE_EXT, i_cur_texture );
1429 glBegin( GL_QUADS );
1431 glTexCoord2f( 0.0, 0.0 );
1432 glVertex2f( - f_x, f_y );
1434 glTexCoord2f( 0.0, (float) p_vout->output.i_height );
1435 glVertex2f( - f_x, - f_y );
1437 glTexCoord2f( (float) p_vout->output.i_width,
1438 (float) p_vout->output.i_height );
1439 glVertex2f( f_x, - f_y );
1441 glTexCoord2f( (float) p_vout->output.i_width, 0.0 );
1442 glVertex2f( f_x, f_y );
1445 /* Wait for the job to be done */
1446 [[self openGLContext] flushBuffer];
1451 /*****************************************************************************
1452 * VLCVout implementation
1453 *****************************************************************************/
1454 @implementation VLCVout
1456 - (void)createWindow:(NSValue *)o_value
1460 NSScreen * o_screen;
1461 vout_thread_t * p_vout;
1462 vlc_bool_t b_main_screen;
1464 p_vout = (vout_thread_t *)[o_value pointerValue];
1466 p_vout->p_sys->o_window = [VLCWindow alloc];
1467 [p_vout->p_sys->o_window setVout: p_vout];
1468 [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1470 if( var_Get( p_vout, "video-device", &val ) < 0 )
1472 o_screen = [NSScreen mainScreen];
1477 NSArray *o_screens = [NSScreen screens];
1478 unsigned int i_index = val.i_int;
1480 if( [o_screens count] < i_index )
1482 o_screen = [NSScreen mainScreen];
1488 o_screen = [o_screens objectAtIndex: i_index];
1489 config_PutInt( p_vout, "macosx-vdev", i_index );
1490 b_main_screen = (i_index == 0);
1494 if( p_vout->p_sys->i_opengl )
1496 /* XXX Fix fullscreen mode */
1497 p_vout->b_fullscreen = 0;
1500 if( p_vout->b_fullscreen )
1502 NSRect screen_rect = [o_screen frame];
1503 screen_rect.origin.x = screen_rect.origin.y = 0;
1505 if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1506 BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1507 NULL, NULL, fullScreenAllowEvents );
1509 [p_vout->p_sys->o_window
1510 initWithContentRect: screen_rect
1511 styleMask: NSBorderlessWindowMask
1512 backing: NSBackingStoreBuffered
1513 defer: NO screen: o_screen];
1515 //[p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1516 p_vout->p_sys->b_mouse_moved = YES;
1517 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1521 unsigned int i_stylemask = NSTitledWindowMask |
1522 NSMiniaturizableWindowMask |
1523 NSClosableWindowMask |
1524 NSResizableWindowMask;
1526 if( !p_vout->p_sys->i_opengl )
1528 if ( p_vout->p_sys->p_fullscreen_state != NULL )
1529 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1530 p_vout->p_sys->p_fullscreen_state = NULL;
1533 [p_vout->p_sys->o_window
1534 initWithContentRect: p_vout->p_sys->s_rect
1535 styleMask: i_stylemask
1536 backing: NSBackingStoreBuffered
1537 defer: NO screen: o_screen];
1539 [p_vout->p_sys->o_window setAlphaValue: config_GetFloat( p_vout, "macosx-opaqueness" )];
1541 if( config_GetInt( p_vout, "video-on-top" ) )
1543 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
1546 if( !p_vout->p_sys->b_pos_saved )
1548 [p_vout->p_sys->o_window center];
1552 if( !p_vout->p_sys->i_opengl )
1554 o_view = [[VLCQTView alloc] init];
1555 /* FIXME: [o_view setMenu:] */
1556 [p_vout->p_sys->o_window setContentView: o_view];
1557 [o_view autorelease];
1560 p_vout->p_sys->p_qdport = [o_view qdPort];
1561 [o_view unlockFocus];
1565 #define o_glview p_vout->p_sys->o_glview
1566 o_glview = [[VLCGLView alloc] initWithFrame: p_vout->p_sys->s_rect vout: p_vout];
1567 [p_vout->p_sys->o_window setContentView: o_glview];
1568 [o_glview autorelease];
1572 [p_vout->p_sys->o_window updateTitle];
1573 [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1577 - (void)destroyWindow:(NSValue *)o_value
1579 vout_thread_t * p_vout;
1581 p_vout = (vout_thread_t *)[o_value pointerValue];
1583 if( !p_vout->b_fullscreen )
1587 s_rect = [[p_vout->p_sys->o_window contentView] frame];
1588 p_vout->p_sys->s_rect.size = s_rect.size;
1590 s_rect = [p_vout->p_sys->o_window frame];
1591 p_vout->p_sys->s_rect.origin = s_rect.origin;
1593 p_vout->p_sys->b_pos_saved = YES;
1596 p_vout->p_sys->p_qdport = nil;
1597 [p_vout->p_sys->o_window close];
1598 p_vout->p_sys->o_window = nil;