1 /*****************************************************************************
2 * vout.m: MacOS X video output module
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
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>
11 * Eric Petit <titer@m0k.org>
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>
37 #include <OpenGL/OpenGL.h>
38 #include <OpenGL/gl.h>
39 #include <OpenGL/glext.h>
46 #define QT_MAX_DIRECTBUFFERS 10
47 #define VL_MAX_DISPLAYS 16
49 #define OPENGL_EFFECT_NONE 1
50 #define OPENGL_EFFECT_CUBE 2
51 #define OPENGL_EFFECT_TRANSPARENT_CUBE 4
58 /* When using I420 output */
59 PlanarPixmapInfoYUV420 pixmap_i420;
62 /*****************************************************************************
64 *****************************************************************************/
66 static int vout_Init ( vout_thread_t * );
67 static void vout_End ( vout_thread_t * );
68 static int vout_Manage ( vout_thread_t * );
69 static void vout_Display ( vout_thread_t *, picture_t * );
71 static int CoSendRequest ( vout_thread_t *, SEL );
72 static int CoCreateWindow ( vout_thread_t * );
73 static int CoDestroyWindow ( vout_thread_t * );
74 static int CoToggleFullscreen ( vout_thread_t * );
76 static void VLCHideMouse ( vout_thread_t *, BOOL );
78 static void QTScaleMatrix ( vout_thread_t * );
79 static int QTCreateSequence ( vout_thread_t * );
80 static void QTDestroySequence ( vout_thread_t * );
81 static int QTNewPicture ( vout_thread_t *, picture_t * );
82 static void QTFreePicture ( vout_thread_t *, picture_t * );
84 /*****************************************************************************
85 * OpenVideo: allocates MacOS X video thread output method
86 *****************************************************************************
87 * This function allocates and initializes a MacOS X vout method.
88 *****************************************************************************/
89 int E_(OpenVideo) ( vlc_object_t *p_this )
91 vout_thread_t * p_vout = (vout_thread_t *)p_this;
95 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
96 if( p_vout->p_sys == NULL )
98 msg_Err( p_vout, "out of memory" );
102 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
104 /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
105 for( i_timeout = 20 ; i_timeout-- ; )
109 msleep( INTF_IDLE_SLEEP );
115 /* no MacOS X intf, unable to communicate with MT */
116 msg_Err( p_vout, "no MacOS X interface present" );
117 free( p_vout->p_sys );
121 if( [NSApp respondsToSelector: @selector(getIntf)] )
123 intf_thread_t * p_intf;
125 for( i_timeout = 10 ; i_timeout-- ; )
127 if( ( p_intf = [NSApp getIntf] ) == NULL )
129 msleep( INTF_IDLE_SLEEP );
135 msg_Err( p_vout, "MacOS X intf has getIntf, but is NULL" );
136 free( p_vout->p_sys );
141 p_vout->p_sys->b_mouse_moved = VLC_TRUE;
142 p_vout->p_sys->i_time_mouse_last_moved = mdate();
144 /* set window size */
145 p_vout->p_sys->s_rect.size.width = p_vout->i_window_width;
146 p_vout->p_sys->s_rect.size.height = p_vout->i_window_height;
148 /* Check if we should use QuickTime or OpenGL */
149 p_vout->p_sys->i_opengl = config_GetInt( p_vout, "macosx-opengl" );
151 if( !p_vout->p_sys->i_opengl )
153 /* Initialize QuickTime */
154 p_vout->p_sys->h_img_descr =
155 (ImageDescriptionHandle)NewHandleClear( sizeof(ImageDescription) );
156 p_vout->p_sys->p_matrix =
157 (MatrixRecordPtr)malloc( sizeof(MatrixRecord) );
158 p_vout->p_sys->p_fullscreen_state = NULL;
160 if( ( err = EnterMovies() ) != noErr )
162 msg_Err( p_vout, "EnterMovies failed: %d", err );
163 free( p_vout->p_sys->p_matrix );
164 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
165 free( p_vout->p_sys );
169 /* Damn QT isn't thread safe. so keep a lock in the p_vlc object */
170 vlc_mutex_lock( &p_vout->p_vlc->quicktime_lock );
172 err = FindCodec( kYUV420CodecType, bestSpeedCodec,
173 nil, &p_vout->p_sys->img_dc );
175 vlc_mutex_unlock( &p_vout->p_vlc->quicktime_lock );
176 if( err == noErr && p_vout->p_sys->img_dc != 0 )
178 p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
179 p_vout->p_sys->i_codec = kYUV420CodecType;
183 msg_Err( p_vout, "failed to find an appropriate codec" );
186 if( p_vout->p_sys->img_dc == 0 )
188 free( p_vout->p_sys->p_matrix );
189 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
190 free( p_vout->p_sys );
195 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
196 NSArray * o_screens = [NSScreen screens];
197 if( [o_screens count] > 0 && var_Type( p_vout, "video-device" ) == 0 )
200 vlc_value_t val, text;
203 int i_option = config_GetInt( p_vout, "macosx-vdev" );
205 var_Create( p_vout, "video-device", VLC_VAR_INTEGER |
207 text.psz_string = _("Video device");
208 var_Change( p_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL );
210 NSEnumerator * o_enumerator = [o_screens objectEnumerator];
212 while( (o_screen = [o_enumerator nextObject]) != NULL )
215 NSRect s_rect = [o_screen frame];
217 snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
218 "%s %d (%dx%d)", _("Screen"), i,
219 (int)s_rect.size.width, (int)s_rect.size.height );
221 text.psz_string = psz_temp;
223 var_Change( p_vout, "video-device",
224 VLC_VAR_ADDCHOICE, &val, &text );
226 if( ( i - 1 ) == i_option )
228 var_Set( p_vout, "video-device", val );
233 var_AddCallback( p_vout, "video-device", vout_VarCallback,
236 val.b_bool = VLC_TRUE;
237 var_Set( p_vout, "intf-change", val );
241 if( CoCreateWindow( p_vout ) )
243 msg_Err( p_vout, "unable to create window" );
244 if( !p_vout->p_sys->i_opengl )
246 free( p_vout->p_sys->p_matrix );
247 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
249 free( p_vout->p_sys );
253 p_vout->pf_init = vout_Init;
254 p_vout->pf_end = vout_End;
255 p_vout->pf_manage = vout_Manage;
256 p_vout->pf_render = NULL;
257 p_vout->pf_display = vout_Display;
262 /*****************************************************************************
263 * vout_Init: initialize video thread output method
264 *****************************************************************************/
265 static int vout_Init( vout_thread_t *p_vout )
270 I_OUTPUTPICTURES = 0;
272 /* Initialize the output structure; we already found a codec,
273 * and the corresponding chroma we will be using. Since we can
274 * arbitrary scale, stick to the coordinates and aspect. */
275 p_vout->output.i_width = p_vout->render.i_width;
276 p_vout->output.i_height = p_vout->render.i_height;
277 p_vout->output.i_aspect = p_vout->render.i_aspect;
279 if( !p_vout->p_sys->i_opengl )
281 SetPort( p_vout->p_sys->p_qdport );
282 QTScaleMatrix( p_vout );
284 if( QTCreateSequence( p_vout ) )
286 msg_Err( p_vout, "unable to create sequence" );
292 p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
293 p_vout->output.i_rmask = 0xFF0000;
294 p_vout->output.i_gmask = 0x00FF00;
295 p_vout->output.i_bmask = 0x0000FF;
298 /* Try to initialize up to QT_MAX_DIRECTBUFFERS direct buffers */
299 while( I_OUTPUTPICTURES <
300 p_vout->p_sys->i_opengl ? 1 : QT_MAX_DIRECTBUFFERS )
304 /* Find an empty picture slot */
305 for( i_index = 0; i_index < VOUT_MAX_PICTURES; i_index++ )
307 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
309 p_pic = p_vout->p_picture + i_index;
314 /* Allocate the picture */
320 if( !p_vout->p_sys->i_opengl )
322 if( QTNewPicture( p_vout, p_pic ) )
329 /* Nothing special to do, we just need a basic allocated
331 vout_AllocatePicture( VLC_OBJECT( p_vout ), p_pic,
332 p_vout->output.i_chroma, p_vout->output.i_width,
333 p_vout->output.i_height, p_vout->output.i_aspect );
336 p_pic->i_status = DESTROYED_PICTURE;
337 p_pic->i_type = DIRECT_PICTURE;
339 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
344 if( p_vout->p_sys->i_opengl )
346 [p_vout->p_sys->o_glview lockFocus];
347 [p_vout->p_sys->o_glview initTextures];
348 [p_vout->p_sys->o_glview reshape];
349 [p_vout->p_sys->o_glview unlockFocus];
355 /*****************************************************************************
356 * vout_End: terminate video thread output method
357 *****************************************************************************/
358 static void vout_End( vout_thread_t *p_vout )
362 if( !p_vout->p_sys->i_opengl )
364 QTDestroySequence( p_vout );
368 [p_vout->p_sys->o_glview cleanUp];
371 /* Free the direct buffers we allocated */
372 for( i_index = I_OUTPUTPICTURES; i_index; )
375 if( !p_vout->p_sys->i_opengl )
377 QTFreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
381 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
386 /*****************************************************************************
387 * CloseVideo: destroy video thread output method
388 *****************************************************************************/
389 void E_(CloseVideo) ( vlc_object_t *p_this )
391 vout_thread_t * p_vout = (vout_thread_t *)p_this;
393 if( CoDestroyWindow( p_vout ) )
395 msg_Err( p_vout, "unable to destroy window" );
398 if( !p_vout->p_sys->i_opengl )
400 if ( p_vout->p_sys->p_fullscreen_state != NULL )
401 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
405 free( p_vout->p_sys->p_matrix );
406 DisposeHandle( (Handle)p_vout->p_sys->h_img_descr );
409 free( p_vout->p_sys );
412 /*****************************************************************************
413 * vout_Manage: handle events
414 *****************************************************************************
415 * This function should be called regularly by video output thread. It manages
416 * console events. It returns a non null value on error.
417 *****************************************************************************/
418 static int vout_Manage( vout_thread_t *p_vout )
420 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
422 if( CoToggleFullscreen( p_vout ) )
427 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
430 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
432 if( !p_vout->p_sys->i_opengl )
434 QTScaleMatrix( p_vout );
435 SetDSequenceMatrix( p_vout->p_sys->i_seq,
436 p_vout->p_sys->p_matrix );
439 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
442 /* hide/show mouse cursor
443 * this code looks unnecessarily complicated, but is necessary like this.
444 * it has to deal with multiple monitors and therefore checks a lot */
445 if( !p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
447 if( mdate() - p_vout->p_sys->i_time_mouse_last_moved > 3000000 )
449 VLCHideMouse( p_vout, YES );
452 else if ( p_vout->p_sys->b_mouse_moved && p_vout->b_fullscreen )
454 VLCHideMouse( p_vout, NO );
457 /* disable screen saver */
458 UpdateSystemActivity( UsrActivity );
463 /*****************************************************************************
464 * vout_Display: displays previously rendered output
465 *****************************************************************************
466 * This function sends the currently rendered image to the display.
467 *****************************************************************************/
468 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
470 if( !p_vout->p_sys->i_opengl )
475 if( ( err = DecompressSequenceFrameS(
476 p_vout->p_sys->i_seq,
477 p_pic->p_sys->p_info,
478 p_pic->p_sys->i_size,
479 codecFlagUseImageBuffer, &flags, nil ) != noErr ) )
481 msg_Warn( p_vout, "DecompressSequenceFrameS failed: %d", err );
485 QDFlushPortBuffer( p_vout->p_sys->p_qdport, nil );
490 if( [p_vout->p_sys->o_glview lockFocusIfCanDraw] )
492 /* Texture gotta be reload before the buffer is filled
493 (thanks to gcc from arstechnica forums) */
494 [p_vout->p_sys->o_glview drawRect:
495 [p_vout->p_sys->o_glview bounds]];
496 [p_vout->p_sys->o_glview reloadTexture];
497 [p_vout->p_sys->o_glview unlockFocus];
502 /*****************************************************************************
503 * CoSendRequest: send request to interface thread
504 *****************************************************************************
505 * Returns 0 on success, 1 otherwise
506 *****************************************************************************/
507 static int CoSendRequest( vout_thread_t *p_vout, SEL sel )
511 intf_thread_t * p_intf;
513 VLCVout * o_vlv = [[VLCVout alloc] init];
515 if( ( i_ret = ExecuteOnMainThread( o_vlv, sel, (void *)p_vout ) ) )
517 msg_Err( p_vout, "SendRequest: no way to communicate with mt" );
522 /*This makes this function dependant of the presence of a macosx
523 interface. We do not check if this interface exists, since it has
524 already been done before.*/
526 p_intf = [NSApp getIntf];
528 val.b_bool = VLC_TRUE;
529 var_Create(p_intf,"intf-change",VLC_VAR_BOOL);
530 var_Set(p_intf, "intf-change",val);
535 /*****************************************************************************
536 * CoCreateWindow: create new window
537 *****************************************************************************
538 * Returns 0 on success, 1 otherwise
539 *****************************************************************************/
540 static int CoCreateWindow( vout_thread_t *p_vout )
542 if( CoSendRequest( p_vout, @selector(createWindow:) ) )
544 msg_Err( p_vout, "CoSendRequest (createWindow) failed" );
551 /*****************************************************************************
552 * CoDestroyWindow: destroy window
553 *****************************************************************************
554 * Returns 0 on success, 1 otherwise
555 *****************************************************************************/
556 static int CoDestroyWindow( vout_thread_t *p_vout )
559 VLCHideMouse( p_vout, NO );
561 if( CoSendRequest( p_vout, @selector(destroyWindow:) ) )
563 msg_Err( p_vout, "CoSendRequest (destroyWindow) failed" );
570 /*****************************************************************************
571 * CoToggleFullscreen: toggle fullscreen
572 *****************************************************************************
573 * Returns 0 on success, 1 otherwise
574 *****************************************************************************/
575 static int CoToggleFullscreen( vout_thread_t *p_vout )
579 intf_thread_t * p_intf;
581 if( p_vout->p_sys->i_opengl )
583 p_vout->b_fullscreen = !p_vout->b_fullscreen;
584 if( p_vout->b_fullscreen )
586 [p_vout->p_sys->o_glview goFullScreen];
590 [p_vout->p_sys->o_glview exitFullScreen];
592 /*This makes this function dependant of the presence of a macosx
593 interface. We do not check if this interface exists, since it has
594 already been done before.*/
596 p_intf = [NSApp getIntf];
598 val.b_bool = VLC_TRUE;
599 var_Create(p_intf,"intf-change",VLC_VAR_BOOL);
600 var_Set(p_intf, "intf-change",val);
605 QTDestroySequence( p_vout );
607 if( CoDestroyWindow( p_vout ) )
609 msg_Err( p_vout, "unable to destroy window" );
613 p_vout->b_fullscreen = !p_vout->b_fullscreen;
615 if( CoCreateWindow( p_vout ) )
617 msg_Err( p_vout, "unable to create window" );
621 SetPort( p_vout->p_sys->p_qdport );
622 QTScaleMatrix( p_vout );
624 if( QTCreateSequence( p_vout ) )
626 msg_Err( p_vout, "unable to create sequence" );
633 /*****************************************************************************
634 * VLCHideMouse: if b_hide then hide the cursor
635 *****************************************************************************/
636 static void VLCHideMouse ( vout_thread_t *p_vout, BOOL b_hide )
641 NSWindow *o_window = p_vout->p_sys->o_window;
642 NSView *o_contents = [o_window contentView];
644 s_rect = [o_contents bounds];
645 ml = [o_window convertScreenToBase:[NSEvent mouseLocation]];
646 ml = [o_contents convertPoint:ml fromView:nil];
647 b_inside = [o_contents mouse: ml inRect: s_rect];
649 if ( b_hide && b_inside )
651 /* only hide if mouse over VLCQTView */
652 [NSCursor setHiddenUntilMouseMoves: YES];
656 [NSCursor setHiddenUntilMouseMoves: NO];
658 p_vout->p_sys->b_mouse_moved = NO;
659 p_vout->p_sys->i_time_mouse_last_moved = mdate();
663 /*****************************************************************************
664 * QTScaleMatrix: scale matrix
665 *****************************************************************************/
666 static void QTScaleMatrix( vout_thread_t *p_vout )
669 unsigned int i_width, i_height;
670 Fixed factor_x, factor_y;
671 unsigned int i_offset_x = 0;
672 unsigned int i_offset_y = 0;
674 GetPortBounds( p_vout->p_sys->p_qdport, &s_rect );
676 i_width = s_rect.right - s_rect.left;
677 i_height = s_rect.bottom - s_rect.top;
679 if( config_GetInt( p_vout, "macosx-stretch" ) )
681 factor_x = FixDiv( Long2Fix( i_width ),
682 Long2Fix( p_vout->output.i_width ) );
683 factor_y = FixDiv( Long2Fix( i_height ),
684 Long2Fix( p_vout->output.i_height ) );
687 else if( i_height * p_vout->output.i_aspect < i_width * VOUT_ASPECT_FACTOR )
689 int i_adj_width = i_height * p_vout->output.i_aspect /
692 factor_x = FixDiv( Long2Fix( i_adj_width ),
693 Long2Fix( p_vout->output.i_width ) );
694 factor_y = FixDiv( Long2Fix( i_height ),
695 Long2Fix( p_vout->output.i_height ) );
697 i_offset_x = (i_width - i_adj_width) / 2;
701 int i_adj_height = i_width * VOUT_ASPECT_FACTOR /
702 p_vout->output.i_aspect;
704 factor_x = FixDiv( Long2Fix( i_width ),
705 Long2Fix( p_vout->output.i_width ) );
706 factor_y = FixDiv( Long2Fix( i_adj_height ),
707 Long2Fix( p_vout->output.i_height ) );
709 i_offset_y = (i_height - i_adj_height) / 2;
712 SetIdentityMatrix( p_vout->p_sys->p_matrix );
714 ScaleMatrix( p_vout->p_sys->p_matrix,
716 Long2Fix(0), Long2Fix(0) );
718 TranslateMatrix( p_vout->p_sys->p_matrix,
719 Long2Fix(i_offset_x), Long2Fix(i_offset_y) );
722 /*****************************************************************************
723 * QTCreateSequence: create a new sequence
724 *****************************************************************************
725 * Returns 0 on success, 1 otherwise
726 *****************************************************************************/
727 static int QTCreateSequence( vout_thread_t *p_vout )
730 ImageDescriptionPtr p_descr;
732 HLock( (Handle)p_vout->p_sys->h_img_descr );
733 p_descr = *p_vout->p_sys->h_img_descr;
735 p_descr->idSize = sizeof(ImageDescription);
736 p_descr->cType = p_vout->p_sys->i_codec;
737 p_descr->version = 1;
738 p_descr->revisionLevel = 0;
739 p_descr->vendor = 'appl';
740 p_descr->width = p_vout->output.i_width;
741 p_descr->height = p_vout->output.i_height;
742 p_descr->hRes = Long2Fix(72);
743 p_descr->vRes = Long2Fix(72);
744 p_descr->spatialQuality = codecLosslessQuality;
745 p_descr->frameCount = 1;
746 p_descr->clutID = -1;
747 p_descr->dataSize = 0;
750 HUnlock( (Handle)p_vout->p_sys->h_img_descr );
752 if( ( err = DecompressSequenceBeginS(
753 &p_vout->p_sys->i_seq,
754 p_vout->p_sys->h_img_descr,
756 p_vout->p_sys->p_qdport,
758 p_vout->p_sys->p_matrix,
760 codecFlagUseImageBuffer,
761 codecLosslessQuality,
762 p_vout->p_sys->img_dc ) ) )
764 msg_Err( p_vout, "DecompressSequenceBeginS failed: %d", err );
771 /*****************************************************************************
772 * QTDestroySequence: destroy sequence
773 *****************************************************************************/
774 static void QTDestroySequence( vout_thread_t *p_vout )
776 CDSequenceEnd( p_vout->p_sys->i_seq );
779 /*****************************************************************************
780 * QTNewPicture: allocate a picture
781 *****************************************************************************
782 * Returns 0 on success, 1 otherwise
783 *****************************************************************************/
784 static int QTNewPicture( vout_thread_t *p_vout, picture_t *p_pic )
786 int i_width = p_vout->output.i_width;
787 int i_height = p_vout->output.i_height;
789 /* We know the chroma, allocate a buffer which will be used
790 * directly by the decoder */
791 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
793 if( p_pic->p_sys == NULL )
798 switch( p_vout->output.i_chroma )
800 case VLC_FOURCC('I','4','2','0'):
802 p_pic->p_sys->p_info = (void *)&p_pic->p_sys->pixmap_i420;
803 p_pic->p_sys->i_size = sizeof(PlanarPixmapInfoYUV420);
805 /* Allocate the memory buffer */
806 p_pic->p_data = vlc_memalign( &p_pic->p_data_orig,
807 16, i_width * i_height * 3 / 2 );
810 p_pic->Y_PIXELS = p_pic->p_data;
811 p_pic->p[Y_PLANE].i_lines = i_height;
812 p_pic->p[Y_PLANE].i_pitch = i_width;
813 p_pic->p[Y_PLANE].i_pixel_pitch = 1;
814 p_pic->p[Y_PLANE].i_visible_pitch = i_width;
817 p_pic->U_PIXELS = p_pic->Y_PIXELS + i_height * i_width;
818 p_pic->p[U_PLANE].i_lines = i_height / 2;
819 p_pic->p[U_PLANE].i_pitch = i_width / 2;
820 p_pic->p[U_PLANE].i_pixel_pitch = 1;
821 p_pic->p[U_PLANE].i_visible_pitch = i_width / 2;
824 p_pic->V_PIXELS = p_pic->U_PIXELS + i_height * i_width / 4;
825 p_pic->p[V_PLANE].i_lines = i_height / 2;
826 p_pic->p[V_PLANE].i_pitch = i_width / 2;
827 p_pic->p[V_PLANE].i_pixel_pitch = 1;
828 p_pic->p[V_PLANE].i_visible_pitch = i_width / 2;
830 /* We allocated 3 planes */
833 #define P p_pic->p_sys->pixmap_i420
834 P.componentInfoY.offset = (void *)p_pic->Y_PIXELS
835 - p_pic->p_sys->p_info;
836 P.componentInfoCb.offset = (void *)p_pic->U_PIXELS
837 - p_pic->p_sys->p_info;
838 P.componentInfoCr.offset = (void *)p_pic->V_PIXELS
839 - p_pic->p_sys->p_info;
841 P.componentInfoY.rowBytes = i_width;
842 P.componentInfoCb.rowBytes = i_width / 2;
843 P.componentInfoCr.rowBytes = i_width / 2;
849 /* Unknown chroma, tell the guy to get lost */
850 free( p_pic->p_sys );
851 msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
852 p_vout->output.i_chroma, (char*)&p_vout->output.i_chroma );
860 /*****************************************************************************
861 * QTFreePicture: destroy a picture allocated with QTNewPicture
862 *****************************************************************************/
863 static void QTFreePicture( vout_thread_t *p_vout, picture_t *p_pic )
865 switch( p_vout->output.i_chroma )
867 case VLC_FOURCC('I','4','2','0'):
868 free( p_pic->p_data_orig );
872 free( p_pic->p_sys );
875 /*****************************************************************************
876 * VLCWindow implementation
877 *****************************************************************************/
878 @implementation VLCWindow
880 - (void)setVout:(vout_thread_t *)_p_vout
885 - (vout_thread_t *)getVout
890 - (void)scaleWindowWithFactor: (float)factor
893 int i_corrected_height, i_corrected_width;
895 NSPoint topleftscreen;
897 if ( !p_vout->b_fullscreen )
900 topleftbase.y = [self frame].size.height;
901 topleftscreen = [self convertBaseToScreen: topleftbase];
903 if( p_vout->output.i_height * p_vout->output.i_aspect >
904 p_vout->output.i_width * VOUT_ASPECT_FACTOR )
906 i_corrected_width = p_vout->output.i_height * p_vout->output.i_aspect /
908 newsize.width = (int) ( i_corrected_width * factor );
909 newsize.height = (int) ( p_vout->render.i_height * factor );
913 i_corrected_height = p_vout->output.i_width * VOUT_ASPECT_FACTOR /
914 p_vout->output.i_aspect;
915 newsize.width = (int) ( p_vout->render.i_width * factor );
916 newsize.height = (int) ( i_corrected_height * factor );
919 [self setContentSize: newsize];
921 [self setFrameTopLeftPoint: topleftscreen];
922 p_vout->i_changes |= VOUT_SIZE_CHANGE;
926 - (void)toggleFloatOnTop
928 if( config_GetInt( p_vout, "video-on-top" ) )
930 config_PutInt( p_vout, "video-on-top", 0 );
931 [p_vout->p_sys->o_window setLevel: NSNormalWindowLevel];
935 config_PutInt( p_vout, "video-on-top", 1 );
936 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
940 - (void)toggleFullscreen
942 config_PutInt(p_vout, "fullscreen", !p_vout->b_fullscreen);
943 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
948 return( p_vout->b_fullscreen );
951 - (BOOL)canBecomeKeyWindow
956 - (void)keyDown:(NSEvent *)o_event
960 unsigned int i_pressed_modifiers = 0;
963 i_pressed_modifiers = [o_event modifierFlags];
965 if( i_pressed_modifiers & NSShiftKeyMask )
966 val.i_int |= KEY_MODIFIER_SHIFT;
967 if( i_pressed_modifiers & NSControlKeyMask )
968 val.i_int |= KEY_MODIFIER_CTRL;
969 if( i_pressed_modifiers & NSAlternateKeyMask )
970 val.i_int |= KEY_MODIFIER_ALT;
971 if( i_pressed_modifiers & NSCommandKeyMask )
972 val.i_int |= KEY_MODIFIER_COMMAND;
974 key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
978 /* Escape should always get you out of fullscreen */
979 if( key == (unichar) 0x1b )
981 if( [self isFullscreen] )
983 [self toggleFullscreen];
986 else if ( key == ' ' )
988 playlist_t *p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
990 if ( p_playlist != NULL )
992 playlist_Pause( p_playlist );
993 vlc_object_release( p_playlist);
998 val.i_int |= CocoaKeyToVLC( key );
999 var_Set( p_vout->p_vlc, "key-pressed", val );
1004 [super keyDown: o_event];
1010 NSMutableString * o_title;
1011 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1014 if( p_playlist == NULL )
1019 vlc_mutex_lock( &p_playlist->object_lock );
1020 o_title = [NSMutableString stringWithUTF8String:
1021 p_playlist->pp_items[p_playlist->i_index]->input.psz_uri];
1022 vlc_mutex_unlock( &p_playlist->object_lock );
1024 vlc_object_release( p_playlist );
1026 if( o_title != nil )
1028 NSRange prefix_range = [o_title rangeOfString: @"file:"];
1029 if( prefix_range.location != NSNotFound )
1031 [o_title deleteCharactersInRange: prefix_range];
1034 [self setTitleWithRepresentedFilename: o_title];
1039 [NSString stringWithCString: VOUT_TITLE " (QuickTime)"]];
1043 /* This is actually the same as VLCControls::stop. */
1044 - (BOOL)windowShouldClose:(id)sender
1046 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1048 if( p_playlist == NULL )
1053 playlist_Stop( p_playlist );
1054 vlc_object_release( p_playlist );
1056 /* The window will be closed by the intf later. */
1062 /*****************************************************************************
1063 * VLCQTView implementation
1064 *****************************************************************************/
1065 @implementation VLCQTView
1067 - (void)drawRect:(NSRect)rect
1069 vout_thread_t * p_vout;
1070 id o_window = [self window];
1071 p_vout = (vout_thread_t *)[o_window getVout];
1073 [[NSColor blackColor] set];
1075 [super drawRect: rect];
1077 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1080 - (BOOL)acceptsFirstResponder
1085 - (BOOL)becomeFirstResponder
1087 vout_thread_t * p_vout;
1088 id o_window = [self window];
1089 p_vout = (vout_thread_t *)[o_window getVout];
1091 [o_window setAcceptsMouseMovedEvents: YES];
1095 - (BOOL)resignFirstResponder
1097 vout_thread_t * p_vout;
1098 id o_window = [self window];
1099 p_vout = (vout_thread_t *)[o_window getVout];
1101 [o_window setAcceptsMouseMovedEvents: NO];
1102 VLCHideMouse( p_vout, NO );
1106 - (void)mouseDown:(NSEvent *)o_event
1108 vout_thread_t * p_vout;
1109 id o_window = [self window];
1110 p_vout = (vout_thread_t *)[o_window getVout];
1113 switch( [o_event type] )
1115 case NSLeftMouseDown:
1117 var_Get( p_vout, "mouse-button-down", &val );
1119 var_Set( p_vout, "mouse-button-down", val );
1124 [super mouseDown: o_event];
1129 - (void)otherMouseDown:(NSEvent *)o_event
1131 /* This is not the the wheel button. you need to poll the
1132 * mouseWheel event for that. other is a third, forth or fifth button */
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] )
1140 case NSOtherMouseDown:
1142 var_Get( p_vout, "mouse-button-down", &val );
1144 var_Set( p_vout, "mouse-button-down", val );
1149 [super mouseDown: o_event];
1154 - (void)rightMouseDown:(NSEvent *)o_event
1156 vout_thread_t * p_vout;
1157 id o_window = [self window];
1158 p_vout = (vout_thread_t *)[o_window getVout];
1161 switch( [o_event type] )
1163 case NSRightMouseDown:
1165 var_Get( p_vout, "mouse-button-down", &val );
1167 var_Set( p_vout, "mouse-button-down", val );
1172 [super mouseDown: o_event];
1177 - (void)mouseUp:(NSEvent *)o_event
1179 vout_thread_t * p_vout;
1180 id o_window = [self window];
1181 p_vout = (vout_thread_t *)[o_window getVout];
1184 switch( [o_event type] )
1189 b_val.b_bool = VLC_TRUE;
1190 var_Set( p_vout, "mouse-clicked", b_val );
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)otherMouseUp:(NSEvent *)o_event
1206 vout_thread_t * p_vout;
1207 id o_window = [self window];
1208 p_vout = (vout_thread_t *)[o_window getVout];
1211 switch( [o_event type] )
1213 case NSOtherMouseUp:
1215 var_Get( p_vout, "mouse-button-down", &val );
1217 var_Set( p_vout, "mouse-button-down", val );
1222 [super mouseUp: o_event];
1227 - (void)rightMouseUp:(NSEvent *)o_event
1229 vout_thread_t * p_vout;
1230 id o_window = [self window];
1231 p_vout = (vout_thread_t *)[o_window getVout];
1234 switch( [o_event type] )
1236 case NSRightMouseUp:
1238 var_Get( p_vout, "mouse-button-down", &val );
1240 var_Set( p_vout, "mouse-button-down", val );
1245 [super mouseUp: o_event];
1250 - (void)mouseDragged:(NSEvent *)o_event
1252 [self mouseMoved:o_event];
1255 - (void)otherMouseDragged:(NSEvent *)o_event
1257 [self mouseMoved:o_event];
1260 - (void)rightMouseDragged:(NSEvent *)o_event
1262 [self mouseMoved:o_event];
1265 - (void)mouseMoved:(NSEvent *)o_event
1271 vout_thread_t * p_vout;
1272 id o_window = [self window];
1273 p_vout = (vout_thread_t *)[o_window getVout];
1275 s_rect = [self bounds];
1276 ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
1277 b_inside = [self mouse: ml inRect: s_rect];
1282 int i_width, i_height, i_x, i_y;
1284 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
1285 (unsigned int)s_rect.size.height,
1286 &i_x, &i_y, &i_width, &i_height );
1288 val.i_int = ( ((int)ml.x) - i_x ) *
1289 p_vout->render.i_width / i_width;
1290 var_Set( p_vout, "mouse-x", val );
1292 val.i_int = ( ((int)ml.y) - i_y ) *
1293 p_vout->render.i_height / i_height;
1294 var_Set( p_vout, "mouse-y", val );
1296 val.b_bool = VLC_TRUE;
1297 var_Set( p_vout, "mouse-moved", val );
1298 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1299 p_vout->p_sys->b_mouse_moved = YES;
1302 [super mouseMoved: o_event];
1307 /*****************************************************************************
1308 * VLCGLView implementation
1309 *****************************************************************************/
1310 @implementation VLCGLView
1313 - (id) initWithFrame: (NSRect) frame vout: (vout_thread_t*) _p_vout
1318 NSOpenGLPixelFormatAttribute attribs[] =
1320 NSOpenGLPFAAccelerated,
1321 NSOpenGLPFANoRecovery,
1322 NSOpenGLPFADoubleBuffer,
1323 NSOpenGLPFAColorSize, 24,
1324 NSOpenGLPFAAlphaSize, 8,
1325 NSOpenGLPFADepthSize, 24,
1330 NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
1331 initWithAttributes: attribs];
1335 msg_Warn( p_vout, "Cannot create NSOpenGLPixelFormat" );
1339 self = [super initWithFrame:frame pixelFormat: fmt];
1341 currentContext = [self openGLContext];
1342 [currentContext makeCurrentContext];
1343 [currentContext update];
1345 /* Black background */
1346 glClearColor( 0.0, 0.0, 0.0, 0.0 );
1348 /* Check if the user asked for useless visual effects */
1349 psz_effect = config_GetPsz( p_vout, "macosx-opengl-effect" );
1350 if( !psz_effect || !strcmp( psz_effect, "none" ))
1352 i_effect = OPENGL_EFFECT_NONE;
1354 else if( !strcmp( psz_effect, "cube" ) )
1356 i_effect = OPENGL_EFFECT_CUBE;
1358 glEnable( GL_DEPTH_TEST );
1360 else if( !strcmp( psz_effect, "transparent-cube" ) )
1362 i_effect = OPENGL_EFFECT_TRANSPARENT_CUBE;
1364 glDisable( GL_DEPTH_TEST );
1365 glEnable( GL_BLEND );
1366 glBlendFunc( GL_SRC_ALPHA, GL_ONE );
1370 msg_Warn( p_vout, "no valid opengl effect provided, using "
1372 i_effect = OPENGL_EFFECT_NONE;
1375 if( i_effect & ( OPENGL_EFFECT_CUBE |
1376 OPENGL_EFFECT_TRANSPARENT_CUBE ) )
1378 /* Set the perpective */
1379 glMatrixMode( GL_PROJECTION );
1381 glFrustum( -1.0, 1.0, -1.0, 1.0, 3.0, 20.0 );
1382 glMatrixMode( GL_MODELVIEW );
1384 glTranslatef( 0.0, 0.0, - 5.0 );
1401 [currentContext makeCurrentContext];
1403 NSRect bounds = [self bounds];
1404 glViewport( 0, 0, (GLint) bounds.size.width,
1405 (GLint) bounds.size.height );
1407 /* Quad size is set in order to preserve the aspect ratio */
1408 if( config_GetInt( p_vout, "macosx-stretch" ) )
1413 else if( bounds.size.height * p_vout->output.i_aspect <
1414 bounds.size.width * VOUT_ASPECT_FACTOR )
1416 f_x = bounds.size.height * p_vout->output.i_aspect /
1417 VOUT_ASPECT_FACTOR / bounds.size.width;
1423 f_y = bounds.size.width * VOUT_ASPECT_FACTOR /
1424 p_vout->output.i_aspect / bounds.size.height;
1428 - (void) initTextures
1430 [currentContext makeCurrentContext];
1432 /* Free previous texture if any */
1435 glDeleteTextures( 1, &i_texture );
1438 /* Create textures */
1439 glGenTextures( 1, &i_texture );
1441 glEnable( GL_TEXTURE_RECTANGLE_EXT );
1442 glEnable( GL_UNPACK_CLIENT_STORAGE_APPLE );
1444 glBindTexture( GL_TEXTURE_RECTANGLE_EXT, i_texture );
1446 /* Use VRAM texturing */
1447 glTexParameteri( GL_TEXTURE_RECTANGLE_EXT,
1448 GL_TEXTURE_STORAGE_HINT_APPLE, GL_STORAGE_CACHED_APPLE );
1450 /* Tell the driver not to make a copy of the texture but to use
1452 glPixelStorei( GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE );
1454 /* Linear interpolation */
1455 glTexParameteri( GL_TEXTURE_RECTANGLE_EXT,
1456 GL_TEXTURE_MIN_FILTER, GL_LINEAR );
1457 glTexParameteri( GL_TEXTURE_RECTANGLE_EXT,
1458 GL_TEXTURE_MAG_FILTER, GL_LINEAR );
1460 /* I have no idea what this exactly does, but it seems to be
1461 necessary for scaling */
1462 glTexParameteri( GL_TEXTURE_RECTANGLE_EXT,
1463 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
1464 glTexParameteri( GL_TEXTURE_RECTANGLE_EXT,
1465 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1466 glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
1468 glTexImage2D( GL_TEXTURE_RECTANGLE_EXT, 0, GL_RGBA,
1469 p_vout->output.i_width, p_vout->output.i_height, 0,
1470 GL_YCBCR_422_APPLE, GL_UNSIGNED_SHORT_8_8_APPLE,
1471 PP_OUTPUTPICTURE[0]->p_data );
1476 - (void) reloadTexture
1483 [currentContext makeCurrentContext];
1485 glBindTexture( GL_TEXTURE_RECTANGLE_EXT, i_texture );
1487 /* glTexSubImage2D is faster than glTexImage2D
1488 http://developer.apple.com/samplecode/Sample_Code/Graphics_3D/
1489 TextureRange/MainOpenGLView.m.htm */
1490 glTexSubImage2D( GL_TEXTURE_RECTANGLE_EXT, 0, 0, 0,
1491 p_vout->output.i_width, p_vout->output.i_height,
1492 GL_YCBCR_422_APPLE, GL_UNSIGNED_SHORT_8_8_APPLE,
1493 PP_OUTPUTPICTURE[0]->p_data );
1496 - (void) goFullScreen
1498 /* Create the new pixel format */
1499 NSOpenGLPixelFormatAttribute attribs[] =
1501 NSOpenGLPFAAccelerated,
1502 NSOpenGLPFANoRecovery,
1503 NSOpenGLPFADoubleBuffer,
1504 NSOpenGLPFAColorSize, 24,
1505 NSOpenGLPFAAlphaSize, 8,
1506 NSOpenGLPFADepthSize, 24,
1507 NSOpenGLPFAFullScreen,
1508 NSOpenGLPFAScreenMask,
1509 /* TODO handle macosxx-vdev */
1510 CGDisplayIDToOpenGLDisplayMask( kCGDirectMainDisplay ),
1513 NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
1514 initWithAttributes: attribs];
1517 msg_Warn( p_vout, "Cannot create NSOpenGLPixelFormat" );
1521 /* Create the new OpenGL context */
1522 fullScreenContext = [[NSOpenGLContext alloc]
1523 initWithFormat: fmt shareContext: nil];
1524 if( !fullScreenContext )
1526 msg_Warn( p_vout, "Failed to create new NSOpenGLContext" );
1529 currentContext = fullScreenContext;
1531 /* Capture display, switch to fullscreen */
1532 if( CGCaptureAllDisplays() != CGDisplayNoErr )
1534 msg_Warn( p_vout, "CGCaptureAllDisplays() failed" );
1537 [fullScreenContext setFullScreen];
1538 [fullScreenContext makeCurrentContext];
1541 unsigned width = CGDisplayPixelsWide( kCGDirectMainDisplay );
1542 unsigned height = CGDisplayPixelsHigh( kCGDirectMainDisplay );
1543 int stretch = config_GetInt( p_vout, "macosx-stretch" );
1544 int fill = config_GetInt( p_vout, "macosx-fill" );
1545 int bigRatio = ( height * p_vout->output.i_aspect <
1546 width * VOUT_ASPECT_FACTOR );
1552 else if( ( bigRatio && !fill ) || ( !bigRatio && fill ) )
1554 f_x = (float) height * p_vout->output.i_aspect /
1555 width / VOUT_ASPECT_FACTOR;
1561 f_y = (float) width * VOUT_ASPECT_FACTOR /
1562 p_vout->output.i_aspect / height;
1565 /* Update viewport, re-init textures */
1566 glViewport( 0, 0, width, height );
1567 [self initTextures];
1569 /* Redraw the last picture */
1570 [self setNeedsDisplay: YES];
1575 - (void) exitFullScreen
1577 /* Free current OpenGL context */
1578 [NSOpenGLContext clearCurrentContext];
1579 [fullScreenContext clearDrawable];
1580 [fullScreenContext release];
1581 CGReleaseAllDisplays();
1583 currentContext = [self openGLContext];
1584 [self initTextures];
1587 /* Redraw the last picture */
1588 [self setNeedsDisplay: YES];
1597 [self exitFullScreen];
1604 glBegin( GL_QUADS );
1606 glTexCoord2f( 0.0, 0.0 );
1607 glVertex2f( - f_x, f_y );
1609 glTexCoord2f( 0.0, (float) p_vout->output.i_height );
1610 glVertex2f( - f_x, - f_y );
1612 glTexCoord2f( (float) p_vout->output.i_width,
1613 (float) p_vout->output.i_height );
1614 glVertex2f( f_x, - f_y );
1616 glTexCoord2f( (float) p_vout->output.i_width, 0.0 );
1617 glVertex2f( f_x, f_y );
1623 glBegin( GL_QUADS );
1625 glTexCoord2f( 0.0, 0.0 );
1626 glVertex3f( - 1.0, 1.0, 1.0 );
1627 glTexCoord2f( 0.0, (float) p_vout->output.i_height );
1628 glVertex3f( - 1.0, - 1.0, 1.0 );
1629 glTexCoord2f( (float) p_vout->output.i_width,
1630 (float) p_vout->output.i_height );
1631 glVertex3f( 1.0, - 1.0, 1.0 );
1632 glTexCoord2f( (float) p_vout->output.i_width, 0.0 );
1633 glVertex3f( 1.0, 1.0, 1.0 );
1636 glTexCoord2f( 0.0, 0.0 );
1637 glVertex3f( - 1.0, 1.0, - 1.0 );
1638 glTexCoord2f( 0.0, (float) p_vout->output.i_height );
1639 glVertex3f( - 1.0, - 1.0, - 1.0 );
1640 glTexCoord2f( (float) p_vout->output.i_width,
1641 (float) p_vout->output.i_height );
1642 glVertex3f( - 1.0, - 1.0, 1.0 );
1643 glTexCoord2f( (float) p_vout->output.i_width, 0.0 );
1644 glVertex3f( - 1.0, 1.0, 1.0 );
1647 glTexCoord2f( 0.0, 0.0 );
1648 glVertex3f( 1.0, 1.0, - 1.0 );
1649 glTexCoord2f( 0.0, (float) p_vout->output.i_height );
1650 glVertex3f( 1.0, - 1.0, - 1.0 );
1651 glTexCoord2f( (float) p_vout->output.i_width,
1652 (float) p_vout->output.i_height );
1653 glVertex3f( - 1.0, - 1.0, - 1.0 );
1654 glTexCoord2f( (float) p_vout->output.i_width, 0.0 );
1655 glVertex3f( - 1.0, 1.0, - 1.0 );
1658 glTexCoord2f( 0.0, 0.0 );
1659 glVertex3f( 1.0, 1.0, 1.0 );
1660 glTexCoord2f( 0.0, (float) p_vout->output.i_height );
1661 glVertex3f( 1.0, - 1.0, 1.0 );
1662 glTexCoord2f( (float) p_vout->output.i_width,
1663 (float) p_vout->output.i_height );
1664 glVertex3f( 1.0, - 1.0, - 1.0 );
1665 glTexCoord2f( (float) p_vout->output.i_width, 0.0 );
1666 glVertex3f( 1.0, 1.0, - 1.0 );
1669 glTexCoord2f( 0.0, 0.0 );
1670 glVertex3f( - 1.0, 1.0, - 1.0 );
1671 glTexCoord2f( 0.0, (float) p_vout->output.i_height );
1672 glVertex3f( - 1.0, 1.0, 1.0 );
1673 glTexCoord2f( (float) p_vout->output.i_width,
1674 (float) p_vout->output.i_height );
1675 glVertex3f( 1.0, 1.0, 1.0 );
1676 glTexCoord2f( (float) p_vout->output.i_width, 0.0 );
1677 glVertex3f( 1.0, 1.0, - 1.0 );
1680 glTexCoord2f( 0.0, 0.0 );
1681 glVertex3f( - 1.0, - 1.0, 1.0 );
1682 glTexCoord2f( 0.0, (float) p_vout->output.i_height );
1683 glVertex3f( - 1.0, - 1.0, - 1.0 );
1684 glTexCoord2f( (float) p_vout->output.i_width,
1685 (float) p_vout->output.i_height );
1686 glVertex3f( 1.0, - 1.0, - 1.0 );
1687 glTexCoord2f( (float) p_vout->output.i_width, 0.0 );
1688 glVertex3f( 1.0, - 1.0, 1.0 );
1692 - (void) drawRect: (NSRect) rect
1694 [currentContext makeCurrentContext];
1696 /* Swap buffers only during the vertical retrace of the monitor.
1697 http://developer.apple.com/documentation/GraphicsImaging/
1698 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
1699 long params[] = { 1 };
1700 CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
1703 /* Black background */
1704 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
1708 [currentContext flushBuffer];
1713 glBindTexture( GL_TEXTURE_RECTANGLE_EXT, i_texture );
1714 if( i_effect & ( OPENGL_EFFECT_CUBE |
1715 OPENGL_EFFECT_TRANSPARENT_CUBE ) )
1717 glRotatef( 1.0, 0.3, 0.5, 0.7 );
1725 /* Wait for the job to be done */
1726 [currentContext flushBuffer];
1731 /*****************************************************************************
1732 * VLCVout implementation
1733 *****************************************************************************/
1734 @implementation VLCVout
1736 - (void)createWindow:(NSValue *)o_value
1740 NSScreen * o_screen;
1741 vout_thread_t * p_vout;
1742 vlc_bool_t b_main_screen;
1744 p_vout = (vout_thread_t *)[o_value pointerValue];
1746 p_vout->p_sys->o_window = [VLCWindow alloc];
1747 [p_vout->p_sys->o_window setVout: p_vout];
1748 [p_vout->p_sys->o_window setReleasedWhenClosed: YES];
1750 if( var_Get( p_vout, "video-device", &val ) < 0 )
1752 o_screen = [NSScreen mainScreen];
1757 NSArray *o_screens = [NSScreen screens];
1758 unsigned int i_index = val.i_int;
1760 if( [o_screens count] < i_index )
1762 o_screen = [NSScreen mainScreen];
1768 o_screen = [o_screens objectAtIndex: i_index];
1769 config_PutInt( p_vout, "macosx-vdev", i_index );
1770 b_main_screen = (i_index == 0);
1774 if( p_vout->b_fullscreen && !p_vout->p_sys->i_opengl )
1776 NSRect screen_rect = [o_screen frame];
1777 screen_rect.origin.x = screen_rect.origin.y = 0;
1779 if ( b_main_screen && p_vout->p_sys->p_fullscreen_state == NULL )
1780 BeginFullScreen( &p_vout->p_sys->p_fullscreen_state, NULL, 0, 0,
1781 NULL, NULL, fullScreenAllowEvents );
1783 [p_vout->p_sys->o_window
1784 initWithContentRect: screen_rect
1785 styleMask: NSBorderlessWindowMask
1786 backing: NSBackingStoreBuffered
1787 defer: NO screen: o_screen];
1789 //[p_vout->p_sys->o_window setLevel: NSPopUpMenuWindowLevel - 1];
1790 p_vout->p_sys->b_mouse_moved = YES;
1791 p_vout->p_sys->i_time_mouse_last_moved = mdate();
1795 unsigned int i_stylemask = NSTitledWindowMask |
1796 NSMiniaturizableWindowMask |
1797 NSClosableWindowMask |
1798 NSResizableWindowMask;
1800 if( !p_vout->p_sys->i_opengl )
1802 if ( p_vout->p_sys->p_fullscreen_state != NULL )
1803 EndFullScreen ( p_vout->p_sys->p_fullscreen_state, NULL );
1804 p_vout->p_sys->p_fullscreen_state = NULL;
1807 [p_vout->p_sys->o_window
1808 initWithContentRect: p_vout->p_sys->s_rect
1809 styleMask: i_stylemask
1810 backing: NSBackingStoreBuffered
1811 defer: NO screen: o_screen];
1813 [p_vout->p_sys->o_window setAlphaValue: config_GetFloat( p_vout, "macosx-opaqueness" )];
1815 if( config_GetInt( p_vout, "video-on-top" ) )
1817 [p_vout->p_sys->o_window setLevel: NSStatusWindowLevel];
1820 if( !p_vout->p_sys->b_pos_saved )
1822 [p_vout->p_sys->o_window center];
1826 if( !p_vout->p_sys->i_opengl )
1828 o_view = [[VLCQTView alloc] init];
1829 /* FIXME: [o_view setMenu:] */
1830 [p_vout->p_sys->o_window setContentView: o_view];
1831 [o_view autorelease];
1834 p_vout->p_sys->p_qdport = [o_view qdPort];
1835 [o_view unlockFocus];
1839 #define o_glview p_vout->p_sys->o_glview
1840 o_glview = [[VLCGLView alloc] initWithFrame: p_vout->p_sys->s_rect vout: p_vout];
1841 [p_vout->p_sys->o_window setContentView: o_glview];
1842 [o_glview autorelease];
1846 [p_vout->p_sys->o_window updateTitle];
1847 [p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
1851 - (void)destroyWindow:(NSValue *)o_value
1853 vout_thread_t * p_vout;
1855 p_vout = (vout_thread_t *)[o_value pointerValue];
1857 if( !p_vout->b_fullscreen )
1861 s_rect = [[p_vout->p_sys->o_window contentView] frame];
1862 p_vout->p_sys->s_rect.size = s_rect.size;
1864 s_rect = [p_vout->p_sys->o_window frame];
1865 p_vout->p_sys->s_rect.origin = s_rect.origin;
1867 p_vout->p_sys->b_pos_saved = YES;
1870 p_vout->p_sys->p_qdport = nil;
1871 [p_vout->p_sys->o_window close];
1872 p_vout->p_sys->o_window = nil;