1 /*****************************************************************************
2 * voutgl.m: MacOS X OpenGL provider
3 *****************************************************************************
4 * Copyright (C) 2001-2004, 2007-2009 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 *****************************************************************************/
30 /*****************************************************************************
32 *****************************************************************************/
33 #include <errno.h> /* ENOMEM */
34 #include <stdlib.h> /* free() */
42 #include <OpenGL/OpenGL.h>
43 #include <OpenGL/gl.h>
47 /*****************************************************************************
49 *****************************************************************************/
50 @interface VLCGLView : NSOpenGLView <VLCVoutViewResetting>
52 vout_thread_t * p_vout;
55 + (void)resetVout: (NSValue *) voutValue;
56 - (id) initWithVout: (vout_thread_t *) p_vout;
62 VLCVoutView * o_vout_view;
67 /* Mozilla plugin-related variables (not 64bit compatible) */
71 AGLDrawable agl_drawable;
73 int i_width, i_height;
75 WindowGroupRef winGroup;
77 Rect clipBounds, viewBounds;
81 /*****************************************************************************
83 *****************************************************************************/
85 static int Init ( vout_thread_t * p_vout );
86 static void End ( vout_thread_t * p_vout );
87 static int Manage ( vout_thread_t * p_vout );
88 static int Control( vout_thread_t *, int, va_list );
89 static void Swap ( vout_thread_t * p_vout );
90 static int Lock ( vout_thread_t * p_vout );
91 static void Unlock ( vout_thread_t * p_vout );
94 static int aglInit ( vout_thread_t * p_vout );
95 static void aglEnd ( vout_thread_t * p_vout );
96 static int aglManage ( vout_thread_t * p_vout );
97 static int aglControl( vout_thread_t *, int, va_list );
98 static void aglSwap ( vout_thread_t * p_vout );
99 static int aglLock ( vout_thread_t * p_vout );
100 static void aglUnlock ( vout_thread_t * p_vout );
103 int OpenVideoGL ( vlc_object_t * p_this )
105 vout_thread_t * p_vout = (vout_thread_t *) p_this;
106 vlc_value_t value_drawable;
108 if( !CGDisplayUsesOpenGLAcceleration( kCGDirectMainDisplay ) )
110 msg_Warn( p_vout, "no OpenGL hardware acceleration found. "
111 "Video display might be slow" );
113 msg_Dbg( p_vout, "display is Quartz Extreme accelerated" );
115 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
116 if( p_vout->p_sys == NULL )
119 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
122 var_Get( p_vout->p_libvlc, "drawable-agl", &value_drawable );
123 if( value_drawable.i_int != 0 )
125 static const GLint ATTRIBUTES[] = {
138 AGLPixelFormat pixFormat;
140 p_vout->p_sys->b_embedded = true;
142 pixFormat = aglChoosePixelFormat(NULL, 0, ATTRIBUTES);
143 if( NULL == pixFormat )
145 msg_Err( p_vout, "no screen renderer available for required attributes." );
146 free( p_vout->p_sys );
150 p_vout->p_sys->agl_ctx = aglCreateContext(pixFormat, NULL);
151 aglDestroyPixelFormat(pixFormat);
152 if( NULL == p_vout->p_sys->agl_ctx )
154 msg_Err( p_vout, "cannot create AGL context." );
155 free( p_vout->p_sys );
160 // tell opengl not to sync buffer swap with vertical retrace (too inefficient)
162 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL, ¶m);
163 aglEnable(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL);
166 p_vout->pf_init = aglInit;
167 p_vout->pf_end = aglEnd;
168 p_vout->pf_manage = aglManage;
169 p_vout->pf_control = aglControl;
170 p_vout->pf_swap = aglSwap;
171 p_vout->pf_lock = aglLock;
172 p_vout->pf_unlock = aglUnlock;
177 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
179 p_vout->p_sys->b_embedded = false;
181 [VLCGLView performSelectorOnMainThread:@selector(initVout:) withObject:[NSValue valueWithPointer:p_vout] waitUntilDone:YES];
185 /* Check to see if initVout: was successfull */
186 if( !p_vout->p_sys->o_vout_view )
188 free( p_vout->p_sys );
192 p_vout->pf_init = Init;
193 p_vout->pf_end = End;
194 p_vout->pf_manage = Manage;
195 p_vout->pf_control= Control;
196 p_vout->pf_swap = Swap;
197 p_vout->pf_lock = Lock;
198 p_vout->pf_unlock = Unlock;
202 p_vout->p_sys->b_got_frame = false;
207 void CloseVideoGL ( vlc_object_t * p_this )
209 vout_thread_t * p_vout = (vout_thread_t *) p_this;
211 msg_Dbg( p_this, "Closing" );
214 /* If the fullscreen window is still open, close it */
215 if( p_vout->b_fullscreen )
217 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
218 if( p_vout->p_sys->b_embedded )
221 var_SetBool( p_vout->p_parent, "fullscreen", false );
227 if( p_vout->p_sys->b_embedded )
229 if( p_vout->p_sys->agl_ctx )
232 aglDestroyContext(p_vout->p_sys->agl_ctx);
235 else if(VLCIntf && vlc_object_alive (VLCIntf))
237 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
239 /* Close the window */
240 [p_vout->p_sys->o_vout_view performSelectorOnMainThread:@selector(closeVout) withObject:NULL waitUntilDone:YES];
245 if(VLCIntf && vlc_object_alive (VLCIntf))
247 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
249 /* Close the window */
250 [p_vout->p_sys->o_vout_view performSelectorOnMainThread:@selector(closeVout) withObject:NULL waitUntilDone:YES];
256 free( p_vout->p_sys );
259 static int Init( vout_thread_t * p_vout )
261 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
265 static void End( vout_thread_t * p_vout )
267 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
270 static int Manage( vout_thread_t * p_vout )
272 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
274 [p_vout->p_sys->o_glview reshape];
275 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
277 if( p_vout->i_changes & VOUT_CROP_CHANGE )
279 [p_vout->p_sys->o_glview reshape];
280 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
283 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
285 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
287 p_vout->b_fullscreen = !p_vout->b_fullscreen;
289 if( p_vout->b_fullscreen )
290 [p_vout->p_sys->o_vout_view enterFullscreen];
292 [p_vout->p_sys->o_vout_view leaveFullscreen];
296 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
299 if( p_vout->p_sys->o_vout_view )
300 [p_vout->p_sys->o_vout_view manage];
304 /*****************************************************************************
305 * Control: control facility for the vout
306 *****************************************************************************/
307 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
313 case VOUT_SET_STAY_ON_TOP:
314 b_arg = (bool) va_arg( args, int );
315 [p_vout->p_sys->o_vout_view setOnTop: b_arg];
323 static void Swap( vout_thread_t * p_vout )
325 p_vout->p_sys->b_got_frame = true;
326 [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
329 static int Lock( vout_thread_t * p_vout )
331 if( kCGLNoError == CGLLockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]) )
333 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
339 static void Unlock( vout_thread_t * p_vout )
341 CGLUnlockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]);
344 /*****************************************************************************
345 * VLCGLView implementation
346 *****************************************************************************/
347 @implementation VLCGLView
348 + (void)initVout:(NSValue *)arg
350 vout_thread_t * p_vout = [arg pointerValue];
352 /* Create the GL view */
353 p_vout->p_sys->o_glview = [[VLCGLView alloc] initWithVout: p_vout];
354 [p_vout->p_sys->o_glview autorelease];
356 /* Spawn the window */
357 id old_vout = p_vout->p_sys->o_vout_view;
358 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
359 subView: p_vout->p_sys->o_glview frame: nil] retain];
363 /* This function will reset the o_vout_view. It's useful to go fullscreen. */
364 + (void)resetVout:(NSValue *) voutValue
366 vout_thread_t * p_vout = [voutValue pointerValue];
367 if( p_vout->b_fullscreen )
369 /* Save window size and position */
370 p_vout->p_sys->s_frame.size =
371 [p_vout->p_sys->o_vout_view frame].size;
372 p_vout->p_sys->s_frame.origin =
373 [[p_vout->p_sys->o_vout_view voutWindow]frame].origin;
374 p_vout->p_sys->b_saved_frame = true;
377 [p_vout->p_sys->o_vout_view closeVout];
379 #define o_glview p_vout->p_sys->o_glview
380 o_glview = [[VLCGLView alloc] initWithVout: p_vout];
381 [o_glview autorelease];
383 if( p_vout->p_sys->b_saved_frame )
385 id old_vout = p_vout->p_sys->o_vout_view;
386 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
388 frame: &p_vout->p_sys->s_frame] retain];
393 id old_vout = p_vout->p_sys->o_vout_view;
394 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
395 subView: o_glview frame: nil] retain];
401 - (id) initWithVout: (vout_thread_t *) vout
403 /* Must be called from main thread:
404 * "The NSView class is generally thread-safe, with a few exceptions. You
405 * should create, destroy, resize, move, and perform other operations on NSView
406 * objects only from the main thread of an application. Drawing from secondary
407 * threads is thread-safe as long as you bracket drawing calls with calls to
408 * lockFocusIfCanDraw and unlockFocus." Cocoa Thread Safety */
412 NSOpenGLPixelFormatAttribute attribs[] =
414 NSOpenGLPFADoubleBuffer,
415 NSOpenGLPFAAccelerated,
416 NSOpenGLPFANoRecovery,
417 NSOpenGLPFAColorSize, 24,
418 NSOpenGLPFAAlphaSize, 8,
419 NSOpenGLPFADepthSize, 24,
424 NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
425 initWithAttributes: attribs];
429 msg_Warn( p_vout, "could not create OpenGL video output" );
433 self = [super initWithFrame: NSMakeRect(0,0,10,10) pixelFormat: fmt];
436 [[self openGLContext] makeCurrentContext];
437 [[self openGLContext] update];
439 /* Swap buffers only during the vertical retrace of the monitor.
440 http://developer.apple.com/documentation/GraphicsImaging/
441 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
442 GLint params[] = { 1 };
443 CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval, params );
447 - (BOOL)mouseDownCanMoveWindow
457 NSRect bounds = [self bounds];
459 if( var_GetBool( p_vout, "macosx-stretch" ) )
461 x = bounds.size.width;
462 y = bounds.size.height;
464 else if( bounds.size.height * p_vout->fmt_in.i_visible_width *
465 p_vout->fmt_in.i_sar_num <
466 bounds.size.width * p_vout->fmt_in.i_visible_height *
467 p_vout->fmt_in.i_sar_den )
469 x = ( bounds.size.height * p_vout->fmt_in.i_visible_width *
470 p_vout->fmt_in.i_sar_num ) /
471 ( p_vout->fmt_in.i_visible_height * p_vout->fmt_in.i_sar_den);
473 y = bounds.size.height;
477 x = bounds.size.width;
478 y = ( bounds.size.width * p_vout->fmt_in.i_visible_height *
479 p_vout->fmt_in.i_sar_den) /
480 ( p_vout->fmt_in.i_visible_width * p_vout->fmt_in.i_sar_num );
483 glViewport( ( bounds.size.width - x ) / 2,
484 ( bounds.size.height - y ) / 2, x, y );
488 if( p_vout->p_sys->b_got_frame )
490 /* Ask the opengl module to redraw */
491 vout_thread_t * p_parent;
492 p_parent = (vout_thread_t *) p_vout->p_parent;
494 if( p_parent && p_parent->pf_display )
496 p_parent->pf_display( p_parent, NULL );
501 glClear( GL_COLOR_BUFFER_BIT );
513 - (void) drawRect: (NSRect) rect
516 [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
517 [super drawRect:rect];
523 /*****************************************************************************
524 * embedded AGL context implementation
525 *****************************************************************************/
529 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds );
530 static void aglReshape( vout_thread_t * p_vout );
531 static OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
533 static int aglInit( vout_thread_t * p_vout )
538 p_vout->p_sys->agl_drawable = (AGLDrawable)
539 var_GetInteger( p_vout->p_libvlc, "drawable-agl" );
540 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
542 viewBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-view-top" );
543 viewBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-view-left" );
544 viewBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-view-bottom" );
545 viewBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-view-right" );
546 clipBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-clip-top" );
547 clipBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-clip-left" );
548 clipBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-clip-bottom" );
549 clipBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-clip-right" );
551 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
552 || (clipBounds.left == clipBounds.right);
553 if( ! p_vout->p_sys->b_clipped_out )
556 aglSetViewport(p_vout, viewBounds, clipBounds);
560 p_vout->p_sys->clipBounds = clipBounds;
561 p_vout->p_sys->viewBounds = viewBounds;
566 static void aglEnd( vout_thread_t * p_vout )
568 aglSetCurrentContext(NULL);
569 if( p_vout->p_sys->theWindow )
571 DisposeWindow( p_vout->p_sys->theWindow );
572 p_vout->p_sys->theWindow = NULL;
576 static void aglReshape( vout_thread_t * p_vout )
579 unsigned int i_height = p_vout->p_sys->i_height;
580 unsigned int i_width = p_vout->p_sys->i_width;
582 vout_PlacePicture(p_vout, i_width, i_height, &x, &y, &i_width, &i_height);
584 glViewport( p_vout->p_sys->i_offx + x, p_vout->p_sys->i_offy + y, i_width, i_height );
586 if( p_vout->p_sys->b_got_frame )
588 /* Ask the opengl module to redraw */
589 vout_thread_t * p_parent;
590 p_parent = (vout_thread_t *) p_vout->p_parent;
591 if( p_parent && p_parent->pf_display )
593 p_parent->pf_display( p_parent, NULL );
598 glClear( GL_COLOR_BUFFER_BIT );
602 /* private event class */
605 kEventClassVLCPlugin = 'vlcp',
607 /* private event kinds */
610 kEventVLCPluginShowFullscreen = 32768,
611 kEventVLCPluginHideFullscreen,
614 static void sendEventToMainThread(EventTargetRef target, UInt32 class, UInt32 kind)
617 if( noErr == CreateEvent(NULL, class, kind, 0, kEventAttributeNone, &myEvent) )
619 if( noErr == SetEventParameter(myEvent, kEventParamPostTarget, typeEventTargetRef, sizeof(EventTargetRef), &target) )
621 PostEventToQueue(GetMainEventQueue(), myEvent, kEventPriorityStandard);
623 ReleaseEvent(myEvent);
627 static int aglManage( vout_thread_t * p_vout )
629 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
634 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
636 if( p_vout->i_changes & VOUT_CROP_CHANGE )
641 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
643 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
645 aglSetDrawable(p_vout->p_sys->agl_ctx, NULL);
647 if( p_vout->b_fullscreen )
649 /* Close the fullscreen window and resume normal drawing */
653 p_vout->p_sys->agl_drawable = (AGLDrawable)
654 var_GetInteger( p_vout->p_libvlc, "drawable-agl" );
656 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
658 viewBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-view-top" );
659 viewBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-view-left" );
660 viewBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-view-bottom" );
661 viewBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-view-right" );
662 clipBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-clip-top" );
663 clipBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-clip-left" );
664 clipBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-clip-bottom" );
665 clipBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-clip-right" );
667 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
668 aglSetViewport(p_vout, viewBounds, clipBounds);
670 if( p_vout->p_sys->theWindow )
672 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty
673 * update to the main thread */
674 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow),
675 kEventClassVLCPlugin, kEventVLCPluginHideFullscreen);
680 /* Go into fullscreen */
683 GDHandle deviceHdl = GetMainDevice();
684 deviceRect = (*deviceHdl)->gdRect;
686 if( !p_vout->p_sys->theWindow )
688 /* Create a window */
689 WindowAttributes windowAttrs;
691 windowAttrs = kWindowStandardDocumentAttributes
692 | kWindowStandardHandlerAttribute
693 | kWindowLiveResizeAttribute
694 | kWindowNoShadowAttribute;
696 windowAttrs &= (~kWindowResizableAttribute);
698 CreateNewWindow(kDocumentWindowClass, windowAttrs, &deviceRect, &p_vout->p_sys->theWindow);
699 if( !p_vout->p_sys->winGroup )
701 CreateWindowGroup(0, &p_vout->p_sys->winGroup);
702 SetWindowGroup(p_vout->p_sys->theWindow, p_vout->p_sys->winGroup);
703 SetWindowGroupParent( p_vout->p_sys->winGroup, GetWindowGroupOfClass(kDocumentWindowClass) ) ;
707 CFStringRef titleKey = CFSTR("Fullscreen VLC media plugin");
708 CFStringRef windowTitle = CFCopyLocalizedString(titleKey, NULL);
709 SetWindowTitleWithCFString(p_vout->p_sys->theWindow, windowTitle);
711 CFRelease(windowTitle);
713 //Install event handler
714 static const EventTypeSpec win_events[] = {
715 { kEventClassMouse, kEventMouseDown },
716 { kEventClassMouse, kEventMouseMoved },
717 { kEventClassMouse, kEventMouseUp },
718 { kEventClassWindow, kEventWindowClosed },
719 { kEventClassWindow, kEventWindowBoundsChanged },
720 { kEventClassCommand, kEventCommandProcess },
721 { kEventClassVLCPlugin, kEventVLCPluginShowFullscreen },
722 { kEventClassVLCPlugin, kEventVLCPluginHideFullscreen },
724 InstallWindowEventHandler (p_vout->p_sys->theWindow, NewEventHandlerUPP (WindowEventHandler), GetEventTypeCount(win_events), win_events, p_vout, NULL);
728 /* just in case device resolution changed */
729 SetWindowBounds(p_vout->p_sys->theWindow, kWindowContentRgn, &deviceRect);
731 glClear( GL_COLOR_BUFFER_BIT );
732 p_vout->p_sys->agl_drawable = (AGLDrawable)GetWindowPort(p_vout->p_sys->theWindow);
733 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
734 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
735 aglSetViewport(p_vout, deviceRect, deviceRect);
736 //aglSetFullScreen(p_vout->p_sys->agl_ctx, device_width, device_height, 0, 0);
738 if( p_vout->p_sys->theWindow )
740 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty
741 * update to the main thread */
742 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow),
743 kEventClassVLCPlugin, kEventVLCPluginShowFullscreen);
748 p_vout->b_fullscreen = !p_vout->b_fullscreen;
749 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
754 static int aglControl( vout_thread_t *p_vout, int i_query, va_list args )
758 case VOUT_SET_VIEWPORT:
760 Rect viewBounds, clipBounds;
761 viewBounds.top = va_arg( args, int);
762 viewBounds.left = va_arg( args, int);
763 viewBounds.bottom = va_arg( args, int);
764 viewBounds.right = va_arg( args, int);
765 clipBounds.top = va_arg( args, int);
766 clipBounds.left = va_arg( args, int);
767 clipBounds.bottom = va_arg( args, int);
768 clipBounds.right = va_arg( args, int);
770 if( !p_vout->b_fullscreen )
773 ** check that the clip rect is not empty, as this is used
774 ** by Firefox to prevent a plugin from displaying during
775 ** a scrolling event. In this case we just prevent buffers
776 ** from being swapped and ignore clipping as this is less
777 ** disruptive than a GL geometry change
780 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
781 || (clipBounds.left == clipBounds.right);
782 if( ! p_vout->p_sys->b_clipped_out )
784 /* ignore consecutive viewport update with identical parameters */
785 if( memcmp(&clipBounds, &(p_vout->p_sys->clipBounds), sizeof(clipBounds) )
786 && memcmp(&viewBounds, &(p_vout->p_sys->viewBounds), sizeof(viewBounds)) )
789 aglSetViewport(p_vout, viewBounds, clipBounds);
790 aglReshape( p_vout );
792 p_vout->p_sys->clipBounds = clipBounds;
793 p_vout->p_sys->viewBounds = viewBounds;
800 case VOUT_REDRAW_RECT:
802 vout_thread_t * p_parent;
805 areaBounds.top = va_arg( args, int);
806 areaBounds.left = va_arg( args, int);
807 areaBounds.bottom = va_arg( args, int);
808 areaBounds.right = va_arg( args, int);
810 /* Ask the opengl module to redraw */
811 p_parent = (vout_thread_t *) p_vout->p_parent;
812 if( p_parent && p_parent->pf_display )
814 p_parent->pf_display( p_parent, NULL );
824 static void aglSwap( vout_thread_t * p_vout )
826 if( ! p_vout->p_sys->b_clipped_out )
828 p_vout->p_sys->b_got_frame = true;
829 aglSwapBuffers(p_vout->p_sys->agl_ctx);
838 /* Enter this function with the p_vout locked */
839 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds )
841 // mozilla plugin provides coordinates based on port bounds
842 // however AGL coordinates are based on window structure region
843 // and are vertically flipped
845 CGrafPtr port = (CGrafPtr)p_vout->p_sys->agl_drawable;
846 Rect winBounds, clientBounds;
848 GetWindowBounds(GetWindowFromPort(port),
849 kWindowStructureRgn, &winBounds);
850 GetWindowBounds(GetWindowFromPort(port),
851 kWindowContentRgn, &clientBounds);
853 /* update video clipping bounds in drawable */
854 rect[0] = (clientBounds.left-winBounds.left)
855 + clipBounds.left; // from window left edge
856 rect[1] = (winBounds.bottom-winBounds.top)
857 - (clientBounds.top-winBounds.top)
858 - clipBounds.bottom; // from window bottom edge
859 rect[2] = clipBounds.right-clipBounds.left; // width
860 rect[3] = clipBounds.bottom-clipBounds.top; // height
862 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT, rect);
863 aglEnable(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT);
865 /* update video internal bounds in drawable */
866 p_vout->p_sys->i_width = viewBounds.right-viewBounds.left;
867 p_vout->p_sys->i_height = viewBounds.bottom-viewBounds.top;
868 p_vout->p_sys->i_offx = -clipBounds.left - viewBounds.left;
869 p_vout->p_sys->i_offy = clipBounds.bottom + viewBounds.top
870 - p_vout->p_sys->i_height;
872 aglUpdateContext(p_vout->p_sys->agl_ctx);
875 //default window event handler
876 static pascal OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData)
878 OSStatus result = noErr;
879 UInt32 class = GetEventClass (event);
880 UInt32 kind = GetEventKind (event);
881 vout_thread_t *p_vout = (vout_thread_t *)userData;
883 result = CallNextEventHandler(nextHandler, event);
884 if(class == kEventClassCommand)
886 HICommand theHICommand;
887 GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL, sizeof( HICommand ), NULL, &theHICommand );
889 switch ( theHICommand.commandID )
892 result = eventNotHandledErr;
895 else if(class == kEventClassWindow)
898 Rect rectPort = {0,0,0,0};
900 GetEventParameter(event, kEventParamDirectObject, typeWindowRef, NULL, sizeof(WindowRef), NULL, &window);
904 GetPortBounds(GetWindowPort(window), &rectPort);
909 case kEventWindowClosed:
910 case kEventWindowZoomed:
911 case kEventWindowBoundsChanged:
915 result = eventNotHandledErr;
918 else if(class == kEventClassMouse)
922 case kEventMouseDown:
926 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
929 case kEventMouseButtonPrimary:
933 var_Get( p_vout, "mouse-button-down", &val );
935 var_Set( p_vout, "mouse-button-down", val );
938 case kEventMouseButtonSecondary:
942 var_Get( p_vout, "mouse-button-down", &val );
944 var_Set( p_vout, "mouse-button-down", val );
947 case kEventMouseButtonTertiary:
951 var_Get( p_vout, "mouse-button-down", &val );
953 var_Set( p_vout, "mouse-button-down", val );
957 result = eventNotHandledErr;
966 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
969 case kEventMouseButtonPrimary:
971 UInt32 clickCount = 0;
972 GetEventParameter(event, kEventParamClickCount, typeUInt32, NULL, sizeof(clickCount), NULL, &clickCount);
978 var_Set((vout_thread_t *) p_vout->p_parent, "fullscreen", val);
984 var_SetBool( p_vout, "mouse-clicked", true );
986 var_Get( p_vout, "mouse-button-down", &val );
988 var_Set( p_vout, "mouse-button-down", val );
992 case kEventMouseButtonSecondary:
996 var_Get( p_vout, "mouse-button-down", &val );
998 var_Set( p_vout, "mouse-button-down", val );
1001 case kEventMouseButtonTertiary:
1005 var_Get( p_vout, "mouse-button-down", &val );
1007 var_Set( p_vout, "mouse-button-down", val );
1011 result = eventNotHandledErr;
1016 case kEventMouseMoved:
1021 unsigned int i_x, i_y;
1022 unsigned int i_height = p_vout->p_sys->i_height;
1023 unsigned int i_width = p_vout->p_sys->i_width;
1025 vout_PlacePicture(p_vout, i_width, i_height, &i_x, &i_y, &i_width, &i_height);
1027 GetEventParameter(event, kEventParamWindowMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &ml);
1029 val.i_int = ( ((int)ml.h) - i_x ) *
1030 p_vout->render.i_width / i_width;
1031 var_Set( p_vout, "mouse-x", val );
1033 val.i_int = ( ((int)ml.v) - i_y ) *
1034 p_vout->render.i_height / i_height;
1036 var_Set( p_vout, "mouse-y", val );
1039 var_Set( p_vout, "mouse-moved", val );
1045 result = eventNotHandledErr;
1048 else if(class == kEventClassTextInput)
1052 case kEventTextInputUnicodeForKeyEvent:
1057 result = eventNotHandledErr;
1060 else if(class == kEventClassVLCPlugin)
1064 case kEventVLCPluginShowFullscreen:
1065 ShowWindow (p_vout->p_sys->theWindow);
1066 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
1067 //CGDisplayHideCursor(kCGDirectMainDisplay);
1069 case kEventVLCPluginHideFullscreen:
1070 HideWindow (p_vout->p_sys->theWindow);
1071 SetSystemUIMode( kUIModeNormal, 0);
1072 CGDisplayShowCursor(kCGDirectMainDisplay);
1075 result = eventNotHandledErr;
1082 static int aglLock( vout_thread_t * p_vout )
1084 /* get the underlying CGL context */
1085 CGLContextObj cglContext;
1086 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
1088 if( kCGLNoError == CGLLockContext( cglContext ) )
1090 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
1097 static void aglUnlock( vout_thread_t * p_vout )
1099 /* get the underlying CGL context */
1100 CGLContextObj cglContext;
1101 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
1103 CGLUnlockContext( cglContext );