1 /*****************************************************************************
2 * voutagl.c: MacOS X agl OpenGL provider (used by webbrowser.plugin)
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
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>
12 * Benjamin Pracht <bigben at videolan dot org>
13 * Damien Fouilleul <damienf at videolan dot org>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
28 *****************************************************************************/
34 /*****************************************************************************
35 * embedded AGL context implementation (not 64bit compatible)
36 *****************************************************************************/
39 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds );
40 static void aglReshape( vout_thread_t * p_vout );
41 static OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
43 int aglInit( vout_thread_t * p_vout )
50 static const GLint ATTRIBUTES[] = {
63 AGLPixelFormat pixFormat;
65 p_vout->p_sys->b_embedded = true;
67 pixFormat = aglChoosePixelFormat(NULL, 0, ATTRIBUTES);
68 if( NULL == pixFormat )
70 msg_Err( p_vout, "No screen renderer available for required attributes." );
74 p_vout->p_sys->agl_ctx = aglCreateContext(pixFormat, NULL);
75 aglDestroyPixelFormat(pixFormat);
76 if( NULL == p_vout->p_sys->agl_ctx )
78 msg_Err( p_vout, "Cannot create AGL context." );
83 // tell opengl not to sync buffer swap with vertical retrace (too inefficient)
85 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL, ¶m);
86 aglEnable(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL);
89 var_Get( p_vout->p_libvlc, "drawable-agl", &val );
90 p_vout->p_sys->agl_drawable = (AGLDrawable)val.i_int;
91 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
93 var_Get( p_vout->p_libvlc, "drawable-view-top", &val );
94 viewBounds.top = val.i_int;
95 var_Get( p_vout->p_libvlc, "drawable-view-left", &val );
96 viewBounds.left = val.i_int;
97 var_Get( p_vout->p_libvlc, "drawable-view-bottom", &val );
98 viewBounds.bottom = val.i_int;
99 var_Get( p_vout->p_libvlc, "drawable-view-right", &val );
100 viewBounds.right = val.i_int;
101 var_Get( p_vout->p_libvlc, "drawable-clip-top", &val );
102 clipBounds.top = val.i_int;
103 var_Get( p_vout->p_libvlc, "drawable-clip-left", &val );
104 clipBounds.left = val.i_int;
105 var_Get( p_vout->p_libvlc, "drawable-clip-bottom", &val );
106 clipBounds.bottom = val.i_int;
107 var_Get( p_vout->p_libvlc, "drawable-clip-right", &val );
108 clipBounds.right = val.i_int;
110 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
111 || (clipBounds.left == clipBounds.right);
112 if( ! p_vout->p_sys->b_clipped_out )
115 aglSetViewport(p_vout, viewBounds, clipBounds);
119 p_vout->p_sys->clipBounds = clipBounds;
120 p_vout->p_sys->viewBounds = viewBounds;
125 void aglEnd( vout_thread_t * p_vout )
127 aglSetCurrentContext(NULL);
128 if( p_vout->p_sys->theWindow )
129 DisposeWindow( p_vout->p_sys->theWindow );
130 aglDestroyContext(p_vout->p_sys->agl_ctx);
133 void aglReshape( vout_thread_t * p_vout )
136 unsigned int i_height = p_vout->p_sys->i_height;
137 unsigned int i_width = p_vout->p_sys->i_width;
139 vout_PlacePicture(p_vout, i_width, i_height, &x, &y, &i_width, &i_height);
141 glViewport( p_vout->p_sys->i_offx + x, p_vout->p_sys->i_offy + y, i_width, i_height );
143 if( p_vout->p_sys->b_got_frame )
145 /* Ask the opengl module to redraw */
146 vout_thread_t * p_parent;
147 p_parent = (vout_thread_t *) p_vout->p_parent;
148 if( p_parent && p_parent->pf_display )
150 p_parent->pf_display( p_parent, NULL );
155 glClear( GL_COLOR_BUFFER_BIT );
159 /* private event class */
162 kEventClassVLCPlugin = 'vlcp',
164 /* private event kinds */
167 kEventVLCPluginShowFullscreen = 32768,
168 kEventVLCPluginHideFullscreen,
171 static void sendEventToMainThread(EventTargetRef target, UInt32 class, UInt32 kind)
174 if( noErr == CreateEvent(NULL, class, kind, 0, kEventAttributeNone, &myEvent) )
176 if( noErr == SetEventParameter(myEvent, kEventParamPostTarget, typeEventTargetRef, sizeof(EventTargetRef), &target) )
178 PostEventToQueue(GetMainEventQueue(), myEvent, kEventPriorityStandard);
180 ReleaseEvent(myEvent);
184 int aglManage( vout_thread_t * p_vout )
186 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
191 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
193 if( p_vout->i_changes & VOUT_CROP_CHANGE )
198 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
200 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
202 aglSetDrawable(p_vout->p_sys->agl_ctx, NULL);
204 if( p_vout->b_fullscreen )
206 /* Close the fullscreen window and resume normal drawing */
211 var_Get( p_vout->p_libvlc, "drawable-agl", &val );
212 p_vout->p_sys->agl_drawable = (AGLDrawable)val.i_int;
213 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
215 var_Get( p_vout->p_libvlc, "drawable-view-top", &val );
216 viewBounds.top = val.i_int;
217 var_Get( p_vout->p_libvlc, "drawable-view-left", &val );
218 viewBounds.left = val.i_int;
219 var_Get( p_vout->p_libvlc, "drawable-view-bottom", &val );
220 viewBounds.bottom = val.i_int;
221 var_Get( p_vout->p_libvlc, "drawable-view-right", &val );
222 viewBounds.right = val.i_int;
223 var_Get( p_vout->p_libvlc, "drawable-clip-top", &val );
224 clipBounds.top = val.i_int;
225 var_Get( p_vout->p_libvlc, "drawable-clip-left", &val );
226 clipBounds.left = val.i_int;
227 var_Get( p_vout->p_libvlc, "drawable-clip-bottom", &val );
228 clipBounds.bottom = val.i_int;
229 var_Get( p_vout->p_libvlc, "drawable-clip-right", &val );
230 clipBounds.right = val.i_int;
232 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
233 aglSetViewport(p_vout, viewBounds, clipBounds);
235 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty update to the main thread */
236 if( p_vout->p_sys->theWindow )
237 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow), kEventClassVLCPlugin, kEventVLCPluginHideFullscreen);
243 GDHandle deviceHdl = GetMainDevice();
244 deviceRect = (*deviceHdl)->gdRect;
246 if( !p_vout->p_sys->theWindow )
248 /* Create a window */
249 WindowAttributes windowAttrs;
251 windowAttrs = kWindowStandardDocumentAttributes
252 | kWindowStandardHandlerAttribute
253 | kWindowLiveResizeAttribute
254 | kWindowNoShadowAttribute;
256 windowAttrs &= (~kWindowResizableAttribute);
258 CreateNewWindow(kDocumentWindowClass, windowAttrs, &deviceRect, &p_vout->p_sys->theWindow);
259 if( !p_vout->p_sys->winGroup )
261 CreateWindowGroup(0, &p_vout->p_sys->winGroup);
262 SetWindowGroup(p_vout->p_sys->theWindow, p_vout->p_sys->winGroup);
263 SetWindowGroupParent( p_vout->p_sys->winGroup, GetWindowGroupOfClass(kDocumentWindowClass) ) ;
267 CFStringRef titleKey = CFSTR("Fullscreen VLC media plugin");
268 CFStringRef windowTitle = CFCopyLocalizedString(titleKey, NULL);
269 SetWindowTitleWithCFString(p_vout->p_sys->theWindow, windowTitle);
271 CFRelease(windowTitle);
273 //Install event handler
274 static const EventTypeSpec win_events[] = {
275 { kEventClassMouse, kEventMouseDown },
276 { kEventClassMouse, kEventMouseMoved },
277 { kEventClassMouse, kEventMouseUp },
278 { kEventClassWindow, kEventWindowClosed },
279 { kEventClassWindow, kEventWindowBoundsChanged },
280 { kEventClassCommand, kEventCommandProcess },
281 { kEventClassVLCPlugin, kEventVLCPluginShowFullscreen },
282 { kEventClassVLCPlugin, kEventVLCPluginHideFullscreen },
284 InstallWindowEventHandler (p_vout->p_sys->theWindow, NewEventHandlerUPP (WindowEventHandler), GetEventTypeCount(win_events), win_events, p_vout, NULL);
288 /* just in case device resolution changed */
289 SetWindowBounds(p_vout->p_sys->theWindow, kWindowContentRgn, &deviceRect);
291 glClear( GL_COLOR_BUFFER_BIT );
292 p_vout->p_sys->agl_drawable = (AGLDrawable)GetWindowPort(p_vout->p_sys->theWindow);
293 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
294 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
295 aglSetViewport(p_vout, deviceRect, deviceRect);
296 //aglSetFullScreen(p_vout->p_sys->agl_ctx, device_width, device_height, 0, 0);
298 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty update to the main thread */
299 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow), kEventClassVLCPlugin, kEventVLCPluginShowFullscreen);
303 p_vout->b_fullscreen = !p_vout->b_fullscreen;
304 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
309 int aglControl( vout_thread_t *p_vout, int i_query, va_list args )
313 case VOUT_SET_VIEWPORT:
315 Rect viewBounds, clipBounds;
316 viewBounds.top = va_arg( args, int);
317 viewBounds.left = va_arg( args, int);
318 viewBounds.bottom = va_arg( args, int);
319 viewBounds.right = va_arg( args, int);
320 clipBounds.top = va_arg( args, int);
321 clipBounds.left = va_arg( args, int);
322 clipBounds.bottom = va_arg( args, int);
323 clipBounds.right = va_arg( args, int);
325 if( !p_vout->b_fullscreen )
328 ** check that the clip rect is not empty, as this is used
329 ** by Firefox to prevent a plugin from displaying during
330 ** a scrolling event. In this case we just prevent buffers
331 ** from being swapped and ignore clipping as this is less
332 ** disruptive than a GL geometry change
335 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
336 || (clipBounds.left == clipBounds.right);
337 if( ! p_vout->p_sys->b_clipped_out )
339 /* ignore consecutive viewport update with identical parameters */
340 if( memcmp(&clipBounds, &(p_vout->p_sys->clipBounds), sizeof(clipBounds) )
341 && memcmp(&viewBounds, &(p_vout->p_sys->viewBounds), sizeof(viewBounds)) )
344 aglSetViewport(p_vout, viewBounds, clipBounds);
345 aglReshape( p_vout );
347 p_vout->p_sys->clipBounds = clipBounds;
348 p_vout->p_sys->viewBounds = viewBounds;
355 case VOUT_REDRAW_RECT:
357 vout_thread_t * p_parent;
360 areaBounds.top = va_arg( args, int);
361 areaBounds.left = va_arg( args, int);
362 areaBounds.bottom = va_arg( args, int);
363 areaBounds.right = va_arg( args, int);
365 /* Ask the opengl module to redraw */
366 p_parent = (vout_thread_t *) p_vout->p_parent;
367 if( p_parent && p_parent->pf_display )
369 p_parent->pf_display( p_parent, NULL );
379 void aglSwap( vout_thread_t * p_vout )
381 if( ! p_vout->p_sys->b_clipped_out )
383 p_vout->p_sys->b_got_frame = true;
384 aglSwapBuffers(p_vout->p_sys->agl_ctx);
393 /* Enter this function with the p_vout locked */
394 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds )
396 // mozilla plugin provides coordinates based on port bounds
397 // however AGL coordinates are based on window structure region
398 // and are vertically flipped
400 CGrafPtr port = (CGrafPtr)p_vout->p_sys->agl_drawable;
401 Rect winBounds, clientBounds;
403 GetWindowBounds(GetWindowFromPort(port),
404 kWindowStructureRgn, &winBounds);
405 GetWindowBounds(GetWindowFromPort(port),
406 kWindowContentRgn, &clientBounds);
408 /* update video clipping bounds in drawable */
409 rect[0] = (clientBounds.left-winBounds.left)
410 + clipBounds.left; // from window left edge
411 rect[1] = (winBounds.bottom-winBounds.top)
412 - (clientBounds.top-winBounds.top)
413 - clipBounds.bottom; // from window bottom edge
414 rect[2] = clipBounds.right-clipBounds.left; // width
415 rect[3] = clipBounds.bottom-clipBounds.top; // height
416 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT, rect);
417 aglEnable(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT);
419 /* update video internal bounds in drawable */
420 p_vout->p_sys->i_width = viewBounds.right-viewBounds.left;
421 p_vout->p_sys->i_height = viewBounds.bottom-viewBounds.top;
422 p_vout->p_sys->i_offx = -clipBounds.left - viewBounds.left;
423 p_vout->p_sys->i_offy = clipBounds.bottom + viewBounds.top
424 - p_vout->p_sys->i_height;
426 aglUpdateContext(p_vout->p_sys->agl_ctx);
429 //default window event handler
430 static pascal OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData)
432 OSStatus result = noErr;
433 UInt32 class = GetEventClass (event);
434 UInt32 kind = GetEventKind (event);
435 vout_thread_t *p_vout = (vout_thread_t *)userData;
437 result = CallNextEventHandler(nextHandler, event);
438 if(class == kEventClassCommand)
440 HICommand theHICommand;
441 GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL, sizeof( HICommand ), NULL, &theHICommand );
443 switch ( theHICommand.commandID )
446 result = eventNotHandledErr;
449 else if(class == kEventClassWindow)
452 Rect rectPort = {0,0,0,0};
454 GetEventParameter(event, kEventParamDirectObject, typeWindowRef, NULL, sizeof(WindowRef), NULL, &window);
458 GetPortBounds(GetWindowPort(window), &rectPort);
463 case kEventWindowClosed:
464 case kEventWindowZoomed:
465 case kEventWindowBoundsChanged:
469 result = eventNotHandledErr;
472 else if(class == kEventClassMouse)
476 case kEventMouseDown:
480 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
483 case kEventMouseButtonPrimary:
487 var_Get( p_vout, "mouse-button-down", &val );
489 var_Set( p_vout, "mouse-button-down", val );
492 case kEventMouseButtonSecondary:
496 var_Get( p_vout, "mouse-button-down", &val );
498 var_Set( p_vout, "mouse-button-down", val );
501 case kEventMouseButtonTertiary:
505 var_Get( p_vout, "mouse-button-down", &val );
507 var_Set( p_vout, "mouse-button-down", val );
511 result = eventNotHandledErr;
520 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
523 case kEventMouseButtonPrimary:
525 UInt32 clickCount = 0;
526 GetEventParameter(event, kEventParamClickCount, typeUInt32, NULL, sizeof(clickCount), NULL, &clickCount);
532 var_Set((vout_thread_t *) p_vout->p_parent, "fullscreen", val);
538 var_SetBool( p_vout, "mouse-clicked", true );
540 var_Get( p_vout, "mouse-button-down", &val );
542 var_Set( p_vout, "mouse-button-down", val );
546 case kEventMouseButtonSecondary:
550 var_Get( p_vout, "mouse-button-down", &val );
552 var_Set( p_vout, "mouse-button-down", val );
555 case kEventMouseButtonTertiary:
559 var_Get( p_vout, "mouse-button-down", &val );
561 var_Set( p_vout, "mouse-button-down", val );
565 result = eventNotHandledErr;
570 case kEventMouseMoved:
575 unsigned int i_x, i_y;
576 unsigned int i_height = p_vout->p_sys->i_height;
577 unsigned int i_width = p_vout->p_sys->i_width;
579 vout_PlacePicture(p_vout, i_width, i_height, &i_x, &i_y, &i_width, &i_height);
581 GetEventParameter(event, kEventParamWindowMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &ml);
583 val.i_int = ( ((int)ml.h) - i_x ) *
584 p_vout->render.i_width / i_width;
585 var_Set( p_vout, "mouse-x", val );
587 val.i_int = ( ((int)ml.v) - i_y ) *
588 p_vout->render.i_height / i_height;
590 var_Set( p_vout, "mouse-y", val );
591 var_TriggerCallback( p_vout, "mouse-moved" );
596 result = eventNotHandledErr;
599 else if(class == kEventClassTextInput)
603 case kEventTextInputUnicodeForKeyEvent:
608 result = eventNotHandledErr;
611 else if(class == kEventClassVLCPlugin)
615 case kEventVLCPluginShowFullscreen:
616 ShowWindow (p_vout->p_sys->theWindow);
617 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
618 //CGDisplayHideCursor(kCGDirectMainDisplay);
620 case kEventVLCPluginHideFullscreen:
621 HideWindow (p_vout->p_sys->theWindow);
622 SetSystemUIMode( kUIModeNormal, 0);
623 CGDisplayShowCursor(kCGDirectMainDisplay);
626 result = eventNotHandledErr;
633 int aglLock( vout_thread_t * p_vout )
637 * before 10.4, we set the AGL context as current and
638 * then we retrieve and use the matching CGL context
640 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
641 return kCGLNoError != CGLLockContext( CGLGetCurrentContext() );
643 /* since 10.4, this is the safe way to get the underlying CGL context */
644 CGLContextObj cglContext;
645 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
647 if( kCGLNoError == CGLLockContext( cglContext ) )
649 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
657 void aglUnlock( vout_thread_t * p_vout )
661 * before 10.4, we assume that the AGL context is current.
662 * therefore, we use the current CGL context
664 CGLUnlockContext( CGLGetCurrentContext() );
666 /* since 10.4, this is the safe way to get the underlying CGL context */
667 CGLContextObj cglContext;
668 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
670 CGLUnlockContext( cglContext );