1 /*****************************************************************************
2 * vout.m: MacOS X video output module
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: vout.m,v 1.75 2004/01/29 02:01:49 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 /* Do this now instead of initWithFrame because we need a
341 first buffer for the glTexImage2D call */
342 [p_vout->p_sys->o_glview initTexture];
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 /* Update the texture with our new picture */
480 [p_vout->p_sys->o_glview reloadTexture: p_pic->p->p_pixels];
484 /*****************************************************************************
485 * CoSendRequest: send request to interface thread
486 *****************************************************************************
487 * Returns 0 on success, 1 otherwise
488 *****************************************************************************/
489 static int CoSendRequest( vout_thread_t *p_vout, SEL sel )
493 intf_thread_t * p_intf;
495 VLCVout * o_vlv = [[VLCVout alloc] init];
497 if( ( i_ret = ExecuteOnMainThread( o_vlv, sel, (void *)p_vout ) ) )
499 msg_Err( p_vout, "SendRequest: no way to communicate with mt" );
504 /*This makes this function dependant of the presence of a macosx
505 interface. We do not check if this interface exists, since it has
506 already been done before.*/
508 p_intf = [NSApp getIntf];
510 val.b_bool = VLC_TRUE;
511 var_Create(p_intf,"intf-change",VLC_VAR_BOOL);
512 var_Set(p_intf, "intf-change",val);
517 /*****************************************************************************
518 * CoCreateWindow: create new window
519 *****************************************************************************
520 * Returns 0 on success, 1 otherwise
521 *****************************************************************************/
522 static int CoCreateWindow( vout_thread_t *p_vout )
524 if( CoSendRequest( p_vout, @selector(createWindow:) ) )
526 msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
533 /*****************************************************************************
534 * CoDestroyWindow: destroy window
535 *****************************************************************************
536 * Returns 0 on success, 1 otherwise
537 *****************************************************************************/
538 static int CoDestroyWindow( vout_thread_t *p_vout )
541 VLCHideMouse( p_vout, NO );
543 if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
545 msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
552 /*****************************************************************************
553 * CoToggleFullscreen: toggle fullscreen
554 *****************************************************************************
555 * Returns 0 on success, 1 otherwise
556 *****************************************************************************/
557 static int CoToggleFullscreen( vout_thread_t *p_vout )
559 if( p_vout->p_sys->i_opengl )
565 QTDestroySequence( p_vout );
567 if( CoDestroyWindow( p_vout ) )
569 msg_Err( p_vout, "unable to destroy window" );
573 p_vout->b_fullscreen = !p_vout->b_fullscreen;
575 if( CoCreateWindow( p_vout ) )
577 msg_Err( p_vout, "unable to create window" );
581 SetPort( p_vout->p_sys->p_qdport );
582 QTScaleMatrix( p_vout );
584 if( QTCreateSequence( p_vout ) )
586 msg_Err( p_vout, "unable to create sequence" );
593 /*****************************************************************************
594 * VLCHideMouse: if b_hide then hide the cursor
595 *****************************************************************************/
596 static void VLCHideMouse ( vout_thread_t *p_vout, BOOL b_hide )
601 NSWindow *o_window = p_vout->p_sys->o_window;
602 NSView *o_contents = [o_window contentView];
604 s_rect = [o_contents bounds];
605 ml = [o_window convertScreenToBase:[NSEvent mouseLocation]];
606 ml = [o_contents convertPoint:ml fromView:nil];
607 b_inside = [o_contents mouse: ml inRect: s_rect];
609 if ( b_hide && b_inside )
611 /* only hide if mouse over VLCQTView */
612 [NSCursor setHiddenUntilMouseMoves: YES];
616 [NSCursor setHiddenUntilMouseMoves: NO];
618 p_vout->p_sys->b_mouse_moved = NO;
619 p_vout->p_sys->i_time_mouse_last_moved = mdate();
623 /*****************************************************************************
624 * QTScaleMatrix: scale matrix
625 *****************************************************************************/
626 static void QTScaleMatrix( vout_thread_t *p_vout )
629 unsigned int i_width, i_height;
630 Fixed factor_x, factor_y;
631 unsigned int i_offset_x = 0;
632 unsigned int i_offset_y = 0;
634 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
636 i_width = s_rect.right - s_rect.left;
637 i_height = s_rect.bottom - s_rect.top;
639 if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
641 int i_adj_width = i_height * p_vout->output.i_aspect /
644 factor_x = FixDiv( Long2Fix( i_adj_width ),
645 Long2Fix( p_vout->output.i_width ) );
646 factor_y = FixDiv( Long2Fix( i_height ),
647 Long2Fix( p_vout->output.i_height ) );
649 i_offset_x = (i_width - i_adj_width) / 2;
653 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
654 p_vout->output.i_aspect;
656 factor_x = FixDiv( Long2Fix( i_width ),
657 Long2Fix( p_vout->output.i_width ) );
658 factor_y = FixDiv( Long2Fix( i_adj_height ),
659 Long2Fix( p_vout->output.i_height ) );
661 i_offset_y = (i_height - i_adj_height) / 2;
664 SetIdentityMatrix( p_vout->p_sys->p_matrix );
666 ScaleMatrix( p_vout->p_sys->p_matrix,
668 Long2Fix(0), Long2Fix(0) );
670 TranslateMatrix( p_vout->p_sys->p_matrix,
671 Long2Fix(i_offset_x),
672 Long2Fix(i_offset_y) );
676 /*****************************************************************************
677 * QTCreateSequence: create a new sequence
678 *****************************************************************************
679 * Returns 0 on success, 1 otherwise
680 *****************************************************************************/
681 static int QTCreateSequence( vout_thread_t *p_vout )
684 ImageDescriptionPtr p_descr;
686 HLock( (Handle)p_vout->p_sys->h_img_descr );
687 p_descr = *p_vout->p_sys->h_img_descr;
689 p_descr->idSize = sizeof(ImageDescription);
690 p_descr->cType = p_vout->p_sys->i_codec;
691 p_descr->version = 1;
692 p_descr->revisionLevel = 0;
693 p_descr->vendor = 'appl';
694 p_descr->width = p_vout->output.i_width;
695 p_descr->height = p_vout->output.i_height;
696 p_descr->hRes = Long2Fix(72);
697 p_descr->vRes = Long2Fix(72);
698 p_descr->spatialQuality = codecLosslessQuality;
699 p_descr->frameCount = 1;
700 p_descr->clutID = -1;
701 p_descr->dataSize = 0;
704 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
706 if( ( err = DecompressSequenceBeginS(
707 &p_vout->p_sys->i_seq,
708 p_vout->p_sys->h_img_descr,
710 p_vout->p_sys->p_qdport,
712 p_vout->p_sys->p_matrix,
714 codecFlagUseImageBuffer,
715 codecLosslessQuality,
716 p_vout->p_sys->img_dc ) ) )
718 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
725 /*****************************************************************************
726 * QTDestroySequence: destroy sequence
727 *****************************************************************************/
728 static void QTDestroySequence( vout_thread_t *p_vout )
730 CDSequenceEnd( p_vout->p_sys->i_seq );
733 /*****************************************************************************
734 * QTNewPicture: allocate a picture
735 *****************************************************************************
736 * Returns 0 on success, 1 otherwise
737 *****************************************************************************/
738 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
740 int i_width = p_vout->output.i_width;
741 int i_height = p_vout->output.i_height;
743 /* We know the chroma, allocate a buffer which will be used
744 * directly by the decoder */
745 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
747 if( p_pic->p_sys == NULL )
752 switch( p_vout->output.i_chroma )
754 case VLC_FOURCC('I','4','2','0'):
756 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
757 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
759 /* Allocate the memory buffer */
760 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
761 16, i_width * i_height * 3 / 2 );
764 p_pic->Y_PIXELS = p_pic->p_data;
765 p_pic->p[Y_PLANE].i_lines = i_height;
766 p_pic->p[Y_PLANE].i_pitch = i_width;
767 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
768 p_pic->p[Y_PLANE].i_visible_pitch = i_width;
771 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
772 p_pic->p[U_PLANE].i_lines = i_height / 2;
773 p_pic->p[U_PLANE].i_pitch = i_width / 2;
774 p_pic->p[U_PLANE].i_pixel_pitch = 1;
775 p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
778 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
779 p_pic->p[V_PLANE].i_lines = i_height / 2;
780 p_pic->p[V_PLANE].i_pitch = i_width / 2;
781 p_pic->p[V_PLANE].i_pixel_pitch = 1;
782 p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
784 /* We allocated 3 planes */
787 #define P p_pic->p_sys->pixmap_i420
788 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
789 - p_pic->p_sys->p_info;
790 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
791 - p_pic->p_sys->p_info;
792 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
793 - p_pic->p_sys->p_info;
795 P.componentInfoY.rowBytes = i_width;
796 P.componentInfoCb.rowBytes = i_width / 2;
797 P.componentInfoCr.rowBytes = i_width / 2;
803 /* Unknown chroma, tell the guy to get lost */
804 free( p_pic->p_sys );
805 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
806 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
814 /*****************************************************************************
815 * QTFreePicture: destroy a picture allocated with QTNewPicture
816 *****************************************************************************/
817 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
819 switch( p_vout->output.i_chroma )
821 case VLC_FOURCC('I','4','2','0'):
822 free( p_pic->p_data_orig );
826 free( p_pic->p_sys );
829 /*****************************************************************************
830 * VLCWindow implementation
831 *****************************************************************************/
832 @implementation VLCWindow
834 - (void)setVout:(vout_thread_t *)_p_vout
839 - (vout_thread_t *)getVout
844 - (void)scaleWindowWithFactor: (float)factor
847 int i_corrected_height, i_corrected_width;
849 NSPoint topleftscreen;
851 if ( !p_vout->b_fullscreen )
854 topleftbase.y = [self frame].size.height;
855 topleftscreen = [self convertBaseToScreen: topleftbase];
857 if( p_vout->output.i_height * p_vout->output.i_aspect >
858 p_vout->output.i_width * VOUT_ASPECT_FACTOR )
860 i_corrected_width = p_vout->output.i_height * p_vout->output.i_aspect /
862 newsize.width = (int) ( i_corrected_width * factor );
863 newsize.height = (int) ( p_vout->render.i_height * factor );
867 i_corrected_height = p_vout->output.i_width * VOUT_ASPECT_FACTOR /
868 p_vout->output.i_aspect;
869 newsize.width = (int) ( p_vout->render.i_width * factor );
870 newsize.height = (int) ( i_corrected_height * factor );
873 [self setContentSize: newsize];
875 [self setFrameTopLeftPoint: topleftscreen];
876 p_vout->i_changes |= VOUT_SIZE_CHANGE;
880 - (void)toggleFloatOnTop
882 if( config_GetInt( p_vout, "video-on-top" ) )
884 config_PutInt( p_vout, "video-on-top", 0 );
885 [p_vout->p_sys->o_window setLevel: NSNormalWindowLevel];
889 config_PutInt( p_vout, "video-on-top", 1 );
890 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
894 - (void)toggleFullscreen
896 config_PutInt(p_vout, "fullscreen", !p_vout->b_fullscreen);
897 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
902 return( p_vout->b_fullscreen );
905 - (BOOL)canBecomeKeyWindow
910 - (void)keyDown:(NSEvent *)o_event
914 unsigned int i_pressed_modifiers = 0;
917 i_pressed_modifiers = [o_event modifierFlags];
919 if( i_pressed_modifiers & NSShiftKeyMask )
920 val.i_int |= KEY_MODIFIER_SHIFT;
921 if( i_pressed_modifiers & NSControlKeyMask )
922 val.i_int |= KEY_MODIFIER_CTRL;
923 if( i_pressed_modifiers & NSAlternateKeyMask )
924 val.i_int |= KEY_MODIFIER_ALT;
925 if( i_pressed_modifiers & NSCommandKeyMask )
926 val.i_int |= KEY_MODIFIER_COMMAND;
928 key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
932 /* Escape should always get you out of fullscreen */
933 if( key == (unichar) 0x1b )
935 if( [self isFullscreen] )
937 [self toggleFullscreen];
940 else if ( key == ' ' )
942 playlist_t *p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
944 if ( p_playlist != NULL )
946 playlist_Pause( p_playlist );
947 vlc_object_release( p_playlist);
952 val.i_int |= CocoaKeyToVLC( key );
953 var_Set( p_vout->p_vlc, "key-pressed", val );
958 [super keyDown: o_event];
964 NSMutableString * o_title;
965 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
968 if( p_playlist == NULL )
973 vlc_mutex_lock( &p_playlist->object_lock );
974 o_title = [NSMutableString stringWithUTF8String:
975 p_playlist->pp_items[p_playlist->i_index]->psz_uri];
976 vlc_mutex_unlock( &p_playlist->object_lock );
978 vlc_object_release( p_playlist );
982 NSRange prefix_range = [o_title rangeOfString: @"file:"];
983 if( prefix_range.location != NSNotFound )
985 [o_title deleteCharactersInRange: prefix_range];
988 [self setTitleWithRepresentedFilename: o_title];
993 [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
997 /* This is actually the same as VLCControls::stop. */
998 - (BOOL)windowShouldClose:(id)sender
1000 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1002 if( p_playlist == NULL )
1007 playlist_Stop( p_playlist );
1008 vlc_object_release( p_playlist );
1010 /* The window will be closed by the intf later. */
1016 /*****************************************************************************
1017 * VLCQTView implementation
1018 *****************************************************************************/
1019 @implementation VLCQTView
1021 - (void)drawRect:(NSRect)rect
1023 vout_thread_t * p_vout;
1024 id o_window = [self window];
1025 p_vout = (vout_thread_t *)[o_window getVout];
1027 [[NSColor blackColor] set];
1029 [super drawRect: rect];
1031 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1034 - (BOOL)acceptsFirstResponder
1039 - (BOOL)becomeFirstResponder
1041 vout_thread_t * p_vout;
1042 id o_window = [self window];
1043 p_vout = (vout_thread_t *)[o_window getVout];
1045 [o_window setAcceptsMouseMovedEvents: YES];
1049 - (BOOL)resignFirstResponder
1051 vout_thread_t * p_vout;
1052 id o_window = [self window];
1053 p_vout = (vout_thread_t *)[o_window getVout];
1055 [o_window setAcceptsMouseMovedEvents: NO];
1056 VLCHideMouse( p_vout, NO );
1060 - (void)mouseDown:(NSEvent *)o_event
1062 vout_thread_t * p_vout;
1063 id o_window = [self window];
1064 p_vout = (vout_thread_t *)[o_window getVout];
1067 switch( [o_event type] )
1069 case NSLeftMouseDown:
1071 var_Get( p_vout, "mouse-button-down", &val );
1073 var_Set( p_vout, "mouse-button-down", val );
1078 [super mouseDown: o_event];
1083 - (void)otherMouseDown:(NSEvent *)o_event
1085 /* This is not the the wheel button. you need to poll the
1086 * mouseWheel event for that. other is a third, forth or fifth button */
1087 vout_thread_t * p_vout;
1088 id o_window = [self window];
1089 p_vout = (vout_thread_t *)[o_window getVout];
1092 switch( [o_event type] )
1094 case NSOtherMouseDown:
1096 var_Get( p_vout, "mouse-button-down", &val );
1098 var_Set( p_vout, "mouse-button-down", val );
1103 [super mouseDown: o_event];
1108 - (void)rightMouseDown:(NSEvent *)o_event
1110 vout_thread_t * p_vout;
1111 id o_window = [self window];
1112 p_vout = (vout_thread_t *)[o_window getVout];
1115 switch( [o_event type] )
1117 case NSRightMouseDown:
1119 var_Get( p_vout, "mouse-button-down", &val );
1121 var_Set( p_vout, "mouse-button-down", val );
1126 [super mouseDown: o_event];
1131 - (void)mouseUp:(NSEvent *)o_event
1133 vout_thread_t * p_vout;
1134 id o_window = [self window];
1135 p_vout = (vout_thread_t *)[o_window getVout];
1138 switch( [o_event type] )
1143 b_val.b_bool = VLC_TRUE;
1144 var_Set( p_vout, "mouse-clicked", b_val );
1146 var_Get( p_vout, "mouse-button-down", &val );
1148 var_Set( p_vout, "mouse-button-down", val );
1153 [super mouseUp: o_event];
1158 - (void)otherMouseUp:(NSEvent *)o_event
1160 vout_thread_t * p_vout;
1161 id o_window = [self window];
1162 p_vout = (vout_thread_t *)[o_window getVout];
1165 switch( [o_event type] )
1167 case NSOtherMouseUp:
1169 var_Get( p_vout, "mouse-button-down", &val );
1171 var_Set( p_vout, "mouse-button-down", val );
1176 [super mouseUp: o_event];
1181 - (void)rightMouseUp:(NSEvent *)o_event
1183 vout_thread_t * p_vout;
1184 id o_window = [self window];
1185 p_vout = (vout_thread_t *)[o_window getVout];
1188 switch( [o_event type] )
1190 case NSRightMouseUp:
1192 var_Get( p_vout, "mouse-button-down", &val );
1194 var_Set( p_vout, "mouse-button-down", val );
1199 [super mouseUp: o_event];
1204 - (void)mouseDragged:(NSEvent *)o_event
1206 [self mouseMoved:o_event];
1209 - (void)otherMouseDragged:(NSEvent *)o_event
1211 [self mouseMoved:o_event];
1214 - (void)rightMouseDragged:(NSEvent *)o_event
1216 [self mouseMoved:o_event];
1219 - (void)mouseMoved:(NSEvent *)o_event
1225 vout_thread_t * p_vout;
1226 id o_window = [self window];
1227 p_vout = (vout_thread_t *)[o_window getVout];
1229 s_rect = [self bounds];
1230 ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1231 b_inside = [self mouse: ml inRect: s_rect];
1236 int i_width, i_height, i_x, i_y;
1238 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1239 (unsigned int)s_rect.size.height,
1240 &i_x, &i_y, &i_width, &i_height );
1242 val.i_int = ( ((int)ml.x) - i_x ) *
1243 p_vout->render.i_width / i_width;
1244 var_Set( p_vout, "mouse-x", val );
1246 val.i_int = ( ((int)ml.y) - i_y ) *
1247 p_vout->render.i_height / i_height;
1248 var_Set( p_vout, "mouse-y", val );
1250 val.b_bool = VLC_TRUE;
1251 var_Set( p_vout, "mouse-moved", val );
1252 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1253 p_vout->p_sys->b_mouse_moved = YES;
1256 [super mouseMoved: o_event];
1261 /*****************************************************************************
1262 * VLCGLView implementation
1263 *****************************************************************************/
1264 @implementation VLCGLView
1267 - (id) initWithFrame: (NSRect) frame
1269 NSOpenGLPixelFormatAttribute attribs[] =
1271 NSOpenGLPFAAccelerated,
1272 NSOpenGLPFANoRecovery,
1273 NSOpenGLPFADoubleBuffer,
1277 NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
1278 initWithAttributes: attribs];
1282 fprintf( stderr, "Cannot create NSOpenGLPixelFormat\n" );
1286 self = [super initWithFrame:frame pixelFormat: fmt];
1288 [[self openGLContext] makeCurrentContext];
1289 [[self openGLContext] update];
1291 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
1292 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
1293 glClearColor( 0.0, 0.0, 0.0, 0.0 );
1302 [[self openGLContext] update];
1303 NSRect bounds = [self bounds];
1304 glViewport( 0, 0, (GLint) bounds.size.width,
1305 (GLint) bounds.size.height );
1308 - (void) initTexture
1310 vout_thread_t * p_vout;
1311 id o_window = [self window];
1312 p_vout = (vout_thread_t *)[o_window getVout];
1316 [[self openGLContext] makeCurrentContext];
1318 /* Swap buffers only during the vertical retrace of the monitor.
1319 http://developer.apple.com/documentation/GraphicsImaging/
1320 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
1321 long params[] = { 1 };
1322 CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
1325 /* Create the texture */
1326 glGenTextures( 1, &i_texture );
1327 glEnable( GL_TEXTURE_RECTANGLE_EXT );
1328 glBindTexture( GL_TEXTURE_RECTANGLE_EXT, i_texture );
1330 /* Turn on AGP transferts */
1331 glTexParameterf( GL_TEXTURE_RECTANGLE_EXT,
1332 GL_TEXTURE_PRIORITY, 0.0 );
1334 /* Use AGP texturing */
1335 glTexParameteri( GL_TEXTURE_RECTANGLE_EXT,
1336 GL_TEXTURE_STORAGE_HINT_APPLE, GL_STORAGE_CACHED_APPLE );
1338 /* Tell the driver not to make a copy of the texture but to use
1340 glPixelStorei( GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE );
1342 /* Linear interpolation */
1343 glTexParameteri( GL_TEXTURE_RECTANGLE_EXT,
1344 GL_TEXTURE_MIN_FILTER, GL_LINEAR );
1345 glTexParameteri( GL_TEXTURE_RECTANGLE_EXT,
1346 GL_TEXTURE_MAG_FILTER, GL_LINEAR );
1348 /* I have no idea what this exactly does, but it seems to be
1349 necessary for scaling */
1350 glTexParameteri( GL_TEXTURE_RECTANGLE_EXT,
1351 GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE );
1352 glTexParameteri( GL_TEXTURE_RECTANGLE_EXT,
1353 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1354 glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
1356 glTexImage2D( GL_TEXTURE_RECTANGLE_EXT, 0, GL_RGBA,
1357 p_vout->output.i_width, p_vout->output.i_height, 0,
1358 GL_YCBCR_422_APPLE, GL_UNSIGNED_SHORT_8_8_APPLE,
1359 PP_OUTPUTPICTURE[0]->p_data );
1366 - (void) reloadTexture: (uint8_t *) buffer
1368 vout_thread_t * p_vout;
1369 id o_window = [self window];
1370 p_vout = (vout_thread_t *)[o_window getVout];
1374 [[self openGLContext] makeCurrentContext];
1375 glBindTexture( GL_TEXTURE_RECTANGLE_EXT, i_texture );
1377 /* glTexSubImage2D is usually faster than glTexImage2D */
1378 glTexSubImage2D( GL_TEXTURE_RECTANGLE_EXT, 0, 0, 0,
1379 p_vout->output.i_width, p_vout->output.i_height,
1380 GL_YCBCR_422_APPLE, GL_UNSIGNED_SHORT_8_8_APPLE,
1382 [self drawRect: [self bounds]];
1387 - (void) drawRect: (NSRect) rect
1389 vout_thread_t * p_vout;
1390 id o_window = [self window];
1391 p_vout = (vout_thread_t *)[o_window getVout];
1393 /* Black background */
1394 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
1399 /* Draw a quad with our texture on it. Quad size is set in order
1400 to preserve the aspect ratio */
1401 NSRect bounds = [self bounds];
1403 if( bounds.size.height * p_vout->output.i_aspect <
1404 bounds.size.width * VOUT_ASPECT_FACTOR )
1406 f_x = bounds.size.height * p_vout->output.i_aspect /
1407 VOUT_ASPECT_FACTOR / bounds.size.width;
1413 f_y = bounds.size.width * VOUT_ASPECT_FACTOR /
1414 p_vout->output.i_aspect / bounds.size.height;
1417 glBindTexture( GL_TEXTURE_RECTANGLE_EXT, i_texture );
1418 glBegin( GL_QUADS );
1420 glTexCoord2f( 0.0f, 0.0f );
1421 glVertex2f( - f_x, f_y );
1423 glTexCoord2f( 0.0f, (float) p_vout->output.i_height );
1424 glVertex2f( - f_x, - f_y );
1426 glTexCoord2f( (float) p_vout->output.i_width,
1427 (float) p_vout->output.i_height );
1428 glVertex2f( f_x, - f_y );
1430 glTexCoord2f( (float) p_vout->output.i_width, 0.0f );
1431 glVertex2f( f_x, f_y );
1434 /* Wait for the job to be done */
1435 [[self openGLContext] flushBuffer];
1440 /*****************************************************************************
1441 * VLCVout implementation
1442 *****************************************************************************/
1443 @implementation VLCVout
1445 - (void)createWindow:(NSValue *)o_value
1449 NSScreen * o_screen;
1450 vout_thread_t * p_vout;
1451 vlc_bool_t b_main_screen;
1453 p_vout = (vout_thread_t *)[o_value pointerValue];
1455 p_vout->p_sys->o_window = [VLCWindow alloc];
1456 [p_vout->p_sys->o_window setVout: p_vout];
1457 [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1459 if( var_Get( p_vout, "video-device", &val ) < 0 )
1461 o_screen = [NSScreen mainScreen];
1466 NSArray *o_screens = [NSScreen screens];
1467 unsigned int i_index = val.i_int;
1469 if( [o_screens count] < i_index )
1471 o_screen = [NSScreen mainScreen];
1477 o_screen = [o_screens objectAtIndex: i_index];
1478 config_PutInt( p_vout, "macosx-vdev", i_index );
1479 b_main_screen = (i_index == 0);
1483 if( p_vout->p_sys->i_opengl )
1485 /* XXX Fix fullscreen mode */
1486 p_vout->b_fullscreen = 0;
1489 if( p_vout->b_fullscreen )
1491 NSRect screen_rect = [o_screen frame];
1492 screen_rect.origin.x = screen_rect.origin.y = 0;
1494 if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1495 BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1496 NULL, NULL, fullScreenAllowEvents );
1498 [p_vout->p_sys->o_window
1499 initWithContentRect: screen_rect
1500 styleMask: NSBorderlessWindowMask
1501 backing: NSBackingStoreBuffered
1502 defer: NO screen: o_screen];
1504 //[p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1505 p_vout->p_sys->b_mouse_moved = YES;
1506 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1510 unsigned int i_stylemask = NSTitledWindowMask |
1511 NSMiniaturizableWindowMask |
1512 NSClosableWindowMask |
1513 NSResizableWindowMask;
1515 if( !p_vout->p_sys->i_opengl )
1517 if ( p_vout->p_sys->p_fullscreen_state != NULL )
1518 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1519 p_vout->p_sys->p_fullscreen_state = NULL;
1522 [p_vout->p_sys->o_window
1523 initWithContentRect: p_vout->p_sys->s_rect
1524 styleMask: i_stylemask
1525 backing: NSBackingStoreBuffered
1526 defer: NO screen: o_screen];
1528 [p_vout->p_sys->o_window setAlphaValue: config_GetFloat( p_vout, "macosx-opaqueness" )];
1530 if( config_GetInt( p_vout, "video-on-top" ) )
1532 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
1535 if( !p_vout->p_sys->b_pos_saved )
1537 [p_vout->p_sys->o_window center];
1541 if( !p_vout->p_sys->i_opengl )
1543 o_view = [[VLCQTView alloc] init];
1544 /* FIXME: [o_view setMenu:] */
1545 [p_vout->p_sys->o_window setContentView: o_view];
1546 [o_view autorelease];
1549 p_vout->p_sys->p_qdport = [o_view qdPort];
1550 [o_view unlockFocus];
1554 #define o_glview p_vout->p_sys->o_glview
1555 o_glview = [[VLCGLView alloc] initWithFrame: p_vout->p_sys->s_rect];
1556 [p_vout->p_sys->o_window setContentView: o_glview];
1557 [o_glview autorelease];
1561 [p_vout->p_sys->o_window updateTitle];
1562 [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1566 - (void)destroyWindow:(NSValue *)o_value
1568 vout_thread_t * p_vout;
1570 p_vout = (vout_thread_t *)[o_value pointerValue];
1572 if( !p_vout->b_fullscreen )
1576 s_rect = [[p_vout->p_sys->o_window contentView] frame];
1577 p_vout->p_sys->s_rect.size = s_rect.size;
1579 s_rect = [p_vout->p_sys->o_window frame];
1580 p_vout->p_sys->s_rect.origin = s_rect.origin;
1582 p_vout->p_sys->b_pos_saved = YES;
1585 p_vout->p_sys->p_qdport = nil;
1586 [p_vout->p_sys->o_window close];
1587 p_vout->p_sys->o_window = nil;