1 /*****************************************************************************
2 * vout.m: MacOS X video output module
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: vout.m 8351 2004-08-02 13:06:38Z hartman $
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 <hartman at videolan dot org>
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() */
40 #include <OpenGL/OpenGL.h>
41 #include <OpenGL/gl.h>
43 /*****************************************************************************
45 *****************************************************************************/
46 @interface VLCGLView : NSOpenGLView
48 vout_thread_t * p_vout;
51 - (id)initWithFrame: (NSRect) frame vout: (vout_thread_t*) p_vout;
58 NSAutoreleasePool * o_pool;
61 vlc_bool_t b_saved_frame;
63 vlc_bool_t * b_got_frame;
66 /*****************************************************************************
68 *****************************************************************************/
70 static int Init ( vout_thread_t * p_vout );
71 static void End ( vout_thread_t * p_vout );
72 static int Manage ( vout_thread_t * p_vout );
73 static int Control( vout_thread_t *, int, va_list );
74 static void Swap ( vout_thread_t * p_vout );
76 int E_(OpenVideoGL) ( vlc_object_t * p_this )
78 vout_thread_t * p_vout = (vout_thread_t *) p_this;
82 /* OpenGL interface disabled until
83 * - the green line is gone
84 * - other problems?????
87 if( !CGDisplayUsesOpenGLAcceleration( kCGDirectMainDisplay ) )
89 msg_Warn( p_vout, "no hardware acceleration" );
92 msg_Dbg( p_vout, "display is Quartz Extreme accelerated" );
94 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
95 if( p_vout->p_sys == NULL )
97 msg_Err( p_vout, "out of memory" );
101 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
103 /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
104 for( i_timeout = 20 ; i_timeout-- ; )
108 msleep( INTF_IDLE_SLEEP );
114 /* No MacOS X intf, unable to communicate with MT */
115 msg_Err( p_vout, "no MacOS X interface present" );
119 p_vout->pf_init = Init;
120 p_vout->pf_end = End;
121 p_vout->pf_manage = Manage;
122 p_vout->pf_control= Control;
123 p_vout->pf_swap = Swap;
126 p_vout->p_sys->o_pool = [[NSAutoreleasePool alloc] init];
128 var_Create( p_vout, "macosx-vdev", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
129 var_Create( p_vout, "macosx-fill", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
130 var_Create( p_vout, "macosx-stretch", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
131 var_Create( p_vout, "macosx-opaqueness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
133 /* Setup the menuitem for the multiple displays. Read the vlc preference (macosx-vdev) for the primary display */
134 NSArray * o_screens = [NSScreen screens];
135 if( [o_screens count] > 0 && var_Type( p_vout, "video-device" ) == 0 )
138 vlc_value_t val2, text;
141 var_Get( p_vout, "macosx-vdev", &val );
143 var_Create( p_vout, "video-device", VLC_VAR_INTEGER |
145 text.psz_string = _("Video device");
146 var_Change( p_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL );
148 NSEnumerator * o_enumerator = [o_screens objectEnumerator];
150 while( (o_screen = [o_enumerator nextObject]) != NULL )
153 NSRect s_rect = [o_screen frame];
155 snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
156 "%s %d (%dx%d)", _("Screen"), i,
157 (int)s_rect.size.width, (int)s_rect.size.height );
159 text.psz_string = psz_temp;
161 var_Change( p_vout, "video-device",
162 VLC_VAR_ADDCHOICE, &val2, &text );
164 if( ( i - 1 ) == val.i_int )
166 var_Set( p_vout, "video-device", val2 );
171 var_AddCallback( p_vout, "video-device", vout_VarCallback,
174 val2.b_bool = VLC_TRUE;
175 var_Set( p_vout, "intf-change", val2 );
179 p_vout->p_sys->b_got_frame = VLC_FALSE;
180 p_vout->p_sys->o_window = [[VLCWindow alloc] initWithVout: p_vout
183 /* Add OpenGL view */
184 #define o_glview p_vout->p_sys->o_glview
185 o_glview = [[VLCGLView alloc] initWithFrame:
186 [p_vout->p_sys->o_window frame] vout: p_vout];
187 [p_vout->p_sys->o_window setContentView: o_glview];
188 [o_glview autorelease];
194 int E_(CloseVideoGL) ( vlc_object_t * p_this )
196 vout_thread_t * p_vout = (vout_thread_t *) p_this;
197 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
199 [p_vout->p_sys->o_window close];
205 static int Init( vout_thread_t * p_vout )
207 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
211 static void End( vout_thread_t * p_vout )
213 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
216 static int Manage( vout_thread_t * p_vout )
218 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
220 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
222 if( !p_vout->b_fullscreen )
224 /* Save window size and position */
225 p_vout->p_sys->s_frame.size =
226 [[p_vout->p_sys->o_window contentView] frame].size;
227 p_vout->p_sys->s_frame.origin =
228 [p_vout->p_sys->o_window frame].origin;
229 p_vout->p_sys->b_saved_frame = VLC_TRUE;
231 [p_vout->p_sys->o_window close];
233 p_vout->b_fullscreen = !p_vout->b_fullscreen;
235 if( p_vout->p_sys->b_saved_frame )
237 p_vout->p_sys->o_window = [[VLCWindow alloc]
238 initWithVout: p_vout frame: &p_vout->p_sys->s_frame];
242 p_vout->p_sys->o_window = [[VLCWindow alloc]
243 initWithVout: p_vout frame: nil];
246 #define o_glview p_vout->p_sys->o_glview
247 o_glview = [[VLCGLView alloc] initWithFrame: [p_vout->p_sys->o_window frame] vout: p_vout];
248 [p_vout->p_sys->o_window setContentView: o_glview];
249 [o_glview autorelease];
250 [[o_glview openGLContext] makeCurrentContext];
254 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
256 [p_vout->p_sys->o_window manage];
260 /*****************************************************************************
261 * Control: control facility for the vout
262 *****************************************************************************/
263 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
269 case VOUT_SET_STAY_ON_TOP:
270 b_arg = va_arg( args, vlc_bool_t );
271 [p_vout->p_sys->o_window setOnTop: b_arg];
277 return vout_vaControlDefault( p_vout, i_query, args );
281 static void Swap( vout_thread_t * p_vout )
283 p_vout->p_sys->b_got_frame = VLC_TRUE;
284 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
285 if( [p_vout->p_sys->o_glview lockFocusIfCanDraw] )
288 [p_vout->p_sys->o_glview unlockFocus];
292 /*****************************************************************************
293 * VLCGLView implementation
294 *****************************************************************************/
295 @implementation VLCGLView
297 - (id) initWithFrame: (NSRect) frame vout: (vout_thread_t*) _p_vout
301 NSOpenGLPixelFormatAttribute attribs[] =
303 NSOpenGLPFAAccelerated,
304 NSOpenGLPFANoRecovery,
305 NSOpenGLPFAColorSize, 24,
306 NSOpenGLPFAAlphaSize, 8,
307 NSOpenGLPFADepthSize, 24,
312 NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
313 initWithAttributes: attribs];
317 msg_Warn( p_vout, "Cannot create NSOpenGLPixelFormat" );
321 self = [super initWithFrame:frame pixelFormat: fmt];
324 [[self openGLContext] makeCurrentContext];
325 [[self openGLContext] update];
327 /* Swap buffers only during the vertical retrace of the monitor.
328 http://developer.apple.com/documentation/GraphicsImaging/
329 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
330 long params[] = { 1 };
331 CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
339 NSRect bounds = [self bounds];
340 [[self openGLContext] makeCurrentContext];
341 if( bounds.size.height * p_vout->render.i_aspect <
342 bounds.size.width * VOUT_ASPECT_FACTOR )
344 x = bounds.size.height * p_vout->render.i_aspect / VOUT_ASPECT_FACTOR;
345 y = bounds.size.height;
349 x = bounds.size.width;
350 y = bounds.size.width * VOUT_ASPECT_FACTOR / p_vout->render.i_aspect;
352 glViewport( ( bounds.size.width - x ) / 2,
353 ( bounds.size.height - y ) / 2, x, y );
355 if( p_vout->p_sys->b_got_frame )
357 /* Ask the opengl module to redraw */
358 vout_thread_t * p_parent;
359 p_parent = (vout_thread_t *) p_vout->p_parent;
360 if( p_parent && p_parent->pf_display )
362 p_parent->pf_display( p_parent, NULL );
367 glClear( GL_COLOR_BUFFER_BIT );
371 - (void) drawRect: (NSRect) rect
373 [[self openGLContext] makeCurrentContext];