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( p_vout->p_sys->b_embedded )
216 /* If the fullscreen window is still open, close it */
217 if( p_vout->b_fullscreen )
219 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
221 var_SetBool( p_vout->p_parent, "fullscreen", false );
223 if( p_vout->p_sys->agl_ctx )
226 aglDestroyContext(p_vout->p_sys->agl_ctx);
231 if(VLCIntf && vlc_object_alive (VLCIntf))
233 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
235 /* Close the window */
236 [p_vout->p_sys->o_vout_view performSelectorOnMainThread:@selector(closeVout) withObject:NULL waitUntilDone:YES];
241 free( p_vout->p_sys );
244 static int Init( vout_thread_t * p_vout )
246 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
250 static void End( vout_thread_t * p_vout )
252 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
255 static int Manage( vout_thread_t * p_vout )
257 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
259 [p_vout->p_sys->o_glview reshape];
260 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
262 if( p_vout->i_changes & VOUT_CROP_CHANGE )
264 [p_vout->p_sys->o_glview reshape];
265 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
268 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
270 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
272 p_vout->b_fullscreen = !p_vout->b_fullscreen;
274 if( p_vout->b_fullscreen )
275 [p_vout->p_sys->o_vout_view enterFullscreen];
277 [p_vout->p_sys->o_vout_view leaveFullscreen];
281 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
284 if( p_vout->p_sys->o_vout_view )
285 [p_vout->p_sys->o_vout_view manage];
289 /*****************************************************************************
290 * Control: control facility for the vout
291 *****************************************************************************/
292 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
298 case VOUT_SET_STAY_ON_TOP:
299 b_arg = (bool) va_arg( args, int );
300 [p_vout->p_sys->o_vout_view setOnTop: b_arg];
308 static void Swap( vout_thread_t * p_vout )
310 p_vout->p_sys->b_got_frame = true;
311 [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
314 static int Lock( vout_thread_t * p_vout )
316 if( kCGLNoError == CGLLockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]) )
318 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
324 static void Unlock( vout_thread_t * p_vout )
326 CGLUnlockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]);
329 /*****************************************************************************
330 * VLCGLView implementation
331 *****************************************************************************/
332 @implementation VLCGLView
333 + (void)initVout:(NSValue *)arg
335 vout_thread_t * p_vout = [arg pointerValue];
337 /* Create the GL view */
338 p_vout->p_sys->o_glview = [[VLCGLView alloc] initWithVout: p_vout];
339 [p_vout->p_sys->o_glview autorelease];
341 /* Spawn the window */
342 id old_vout = p_vout->p_sys->o_vout_view;
343 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
344 subView: p_vout->p_sys->o_glview frame: nil] retain];
348 /* This function will reset the o_vout_view. It's useful to go fullscreen. */
349 + (void)resetVout:(NSValue *) voutValue
351 vout_thread_t * p_vout = [voutValue pointerValue];
352 if( p_vout->b_fullscreen )
354 /* Save window size and position */
355 p_vout->p_sys->s_frame.size =
356 [p_vout->p_sys->o_vout_view frame].size;
357 p_vout->p_sys->s_frame.origin =
358 [[p_vout->p_sys->o_vout_view voutWindow]frame].origin;
359 p_vout->p_sys->b_saved_frame = true;
362 [p_vout->p_sys->o_vout_view closeVout];
364 #define o_glview p_vout->p_sys->o_glview
365 o_glview = [[VLCGLView alloc] initWithVout: p_vout];
366 [o_glview autorelease];
368 if( p_vout->p_sys->b_saved_frame )
370 id old_vout = p_vout->p_sys->o_vout_view;
371 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
373 frame: &p_vout->p_sys->s_frame] retain];
378 id old_vout = p_vout->p_sys->o_vout_view;
379 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
380 subView: o_glview frame: nil] retain];
386 - (id) initWithVout: (vout_thread_t *) vout
388 /* Must be called from main thread:
389 * "The NSView class is generally thread-safe, with a few exceptions. You
390 * should create, destroy, resize, move, and perform other operations on NSView
391 * objects only from the main thread of an application. Drawing from secondary
392 * threads is thread-safe as long as you bracket drawing calls with calls to
393 * lockFocusIfCanDraw and unlockFocus." Cocoa Thread Safety */
397 NSOpenGLPixelFormatAttribute attribs[] =
399 NSOpenGLPFADoubleBuffer,
400 NSOpenGLPFAAccelerated,
401 NSOpenGLPFANoRecovery,
402 NSOpenGLPFAColorSize, 24,
403 NSOpenGLPFAAlphaSize, 8,
404 NSOpenGLPFADepthSize, 24,
409 NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
410 initWithAttributes: attribs];
414 msg_Warn( p_vout, "could not create OpenGL video output" );
418 self = [super initWithFrame: NSMakeRect(0,0,10,10) pixelFormat: fmt];
421 [[self openGLContext] makeCurrentContext];
422 [[self openGLContext] update];
424 /* Swap buffers only during the vertical retrace of the monitor.
425 http://developer.apple.com/documentation/GraphicsImaging/
426 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
427 GLint params[] = { 1 };
428 CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval, params );
432 - (BOOL)mouseDownCanMoveWindow
442 NSRect bounds = [self bounds];
444 if( var_GetBool( p_vout, "macosx-stretch" ) )
446 x = bounds.size.width;
447 y = bounds.size.height;
449 else if( bounds.size.height * p_vout->fmt_in.i_visible_width *
450 p_vout->fmt_in.i_sar_num <
451 bounds.size.width * p_vout->fmt_in.i_visible_height *
452 p_vout->fmt_in.i_sar_den )
454 x = ( bounds.size.height * p_vout->fmt_in.i_visible_width *
455 p_vout->fmt_in.i_sar_num ) /
456 ( p_vout->fmt_in.i_visible_height * p_vout->fmt_in.i_sar_den);
458 y = bounds.size.height;
462 x = bounds.size.width;
463 y = ( bounds.size.width * p_vout->fmt_in.i_visible_height *
464 p_vout->fmt_in.i_sar_den) /
465 ( p_vout->fmt_in.i_visible_width * p_vout->fmt_in.i_sar_num );
468 glViewport( ( bounds.size.width - x ) / 2,
469 ( bounds.size.height - y ) / 2, x, y );
473 if( p_vout->p_sys->b_got_frame )
475 /* Ask the opengl module to redraw */
476 vout_thread_t * p_parent;
477 p_parent = (vout_thread_t *) p_vout->p_parent;
479 if( p_parent && p_parent->pf_display )
481 p_parent->pf_display( p_parent, NULL );
486 glClear( GL_COLOR_BUFFER_BIT );
498 - (void) drawRect: (NSRect) rect
501 [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
502 [super drawRect:rect];
508 /*****************************************************************************
509 * embedded AGL context implementation
510 *****************************************************************************/
514 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds );
515 static void aglReshape( vout_thread_t * p_vout );
516 static OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
518 static int aglInit( vout_thread_t * p_vout )
523 p_vout->p_sys->agl_drawable = (AGLDrawable)
524 var_GetInteger( p_vout->p_libvlc, "drawable-agl" );
525 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
527 viewBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-view-top" );
528 viewBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-view-left" );
529 viewBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-view-bottom" );
530 viewBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-view-right" );
531 clipBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-clip-top" );
532 clipBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-clip-left" );
533 clipBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-clip-bottom" );
534 clipBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-clip-right" );
536 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
537 || (clipBounds.left == clipBounds.right);
538 if( ! p_vout->p_sys->b_clipped_out )
541 aglSetViewport(p_vout, viewBounds, clipBounds);
545 p_vout->p_sys->clipBounds = clipBounds;
546 p_vout->p_sys->viewBounds = viewBounds;
551 static void aglEnd( vout_thread_t * p_vout )
553 aglSetCurrentContext(NULL);
554 if( p_vout->p_sys->theWindow )
556 DisposeWindow( p_vout->p_sys->theWindow );
557 p_vout->p_sys->theWindow = NULL;
561 static void aglReshape( vout_thread_t * p_vout )
564 unsigned int i_height = p_vout->p_sys->i_height;
565 unsigned int i_width = p_vout->p_sys->i_width;
567 vout_PlacePicture(p_vout, i_width, i_height, &x, &y, &i_width, &i_height);
569 glViewport( p_vout->p_sys->i_offx + x, p_vout->p_sys->i_offy + y, i_width, i_height );
571 if( p_vout->p_sys->b_got_frame )
573 /* Ask the opengl module to redraw */
574 vout_thread_t * p_parent;
575 p_parent = (vout_thread_t *) p_vout->p_parent;
576 if( p_parent && p_parent->pf_display )
578 p_parent->pf_display( p_parent, NULL );
583 glClear( GL_COLOR_BUFFER_BIT );
587 /* private event class */
590 kEventClassVLCPlugin = 'vlcp',
592 /* private event kinds */
595 kEventVLCPluginShowFullscreen = 32768,
596 kEventVLCPluginHideFullscreen,
599 static void sendEventToMainThread(EventTargetRef target, UInt32 class, UInt32 kind)
602 if( noErr == CreateEvent(NULL, class, kind, 0, kEventAttributeNone, &myEvent) )
604 if( noErr == SetEventParameter(myEvent, kEventParamPostTarget, typeEventTargetRef, sizeof(EventTargetRef), &target) )
606 PostEventToQueue(GetMainEventQueue(), myEvent, kEventPriorityStandard);
608 ReleaseEvent(myEvent);
612 static int aglManage( vout_thread_t * p_vout )
614 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
619 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
621 if( p_vout->i_changes & VOUT_CROP_CHANGE )
626 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
628 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
630 aglSetDrawable(p_vout->p_sys->agl_ctx, NULL);
632 if( p_vout->b_fullscreen )
634 /* Close the fullscreen window and resume normal drawing */
638 p_vout->p_sys->agl_drawable = (AGLDrawable)
639 var_GetInteger( p_vout->p_libvlc, "drawable-agl" );
641 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
643 viewBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-view-top" );
644 viewBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-view-left" );
645 viewBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-view-bottom" );
646 viewBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-view-right" );
647 clipBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-clip-top" );
648 clipBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-clip-left" );
649 clipBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-clip-bottom" );
650 clipBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-clip-right" );
652 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
653 aglSetViewport(p_vout, viewBounds, clipBounds);
655 if( p_vout->p_sys->theWindow )
657 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty
658 * update to the main thread */
659 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow),
660 kEventClassVLCPlugin, kEventVLCPluginHideFullscreen);
665 /* Go into fullscreen */
668 GDHandle deviceHdl = GetMainDevice();
669 deviceRect = (*deviceHdl)->gdRect;
671 if( !p_vout->p_sys->theWindow )
673 /* Create a window */
674 WindowAttributes windowAttrs;
676 windowAttrs = kWindowStandardDocumentAttributes
677 | kWindowStandardHandlerAttribute
678 | kWindowLiveResizeAttribute
679 | kWindowNoShadowAttribute;
681 windowAttrs &= (~kWindowResizableAttribute);
683 CreateNewWindow(kDocumentWindowClass, windowAttrs, &deviceRect, &p_vout->p_sys->theWindow);
684 if( !p_vout->p_sys->winGroup )
686 CreateWindowGroup(0, &p_vout->p_sys->winGroup);
687 SetWindowGroup(p_vout->p_sys->theWindow, p_vout->p_sys->winGroup);
688 SetWindowGroupParent( p_vout->p_sys->winGroup, GetWindowGroupOfClass(kDocumentWindowClass) ) ;
692 CFStringRef titleKey = CFSTR("Fullscreen VLC media plugin");
693 CFStringRef windowTitle = CFCopyLocalizedString(titleKey, NULL);
694 SetWindowTitleWithCFString(p_vout->p_sys->theWindow, windowTitle);
696 CFRelease(windowTitle);
698 //Install event handler
699 static const EventTypeSpec win_events[] = {
700 { kEventClassMouse, kEventMouseDown },
701 { kEventClassMouse, kEventMouseMoved },
702 { kEventClassMouse, kEventMouseUp },
703 { kEventClassWindow, kEventWindowClosed },
704 { kEventClassWindow, kEventWindowBoundsChanged },
705 { kEventClassCommand, kEventCommandProcess },
706 { kEventClassVLCPlugin, kEventVLCPluginShowFullscreen },
707 { kEventClassVLCPlugin, kEventVLCPluginHideFullscreen },
709 InstallWindowEventHandler (p_vout->p_sys->theWindow, NewEventHandlerUPP (WindowEventHandler), GetEventTypeCount(win_events), win_events, p_vout, NULL);
713 /* just in case device resolution changed */
714 SetWindowBounds(p_vout->p_sys->theWindow, kWindowContentRgn, &deviceRect);
716 glClear( GL_COLOR_BUFFER_BIT );
717 p_vout->p_sys->agl_drawable = (AGLDrawable)GetWindowPort(p_vout->p_sys->theWindow);
718 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
719 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
720 aglSetViewport(p_vout, deviceRect, deviceRect);
721 //aglSetFullScreen(p_vout->p_sys->agl_ctx, device_width, device_height, 0, 0);
723 if( p_vout->p_sys->theWindow )
725 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty
726 * update to the main thread */
727 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow),
728 kEventClassVLCPlugin, kEventVLCPluginShowFullscreen);
733 p_vout->b_fullscreen = !p_vout->b_fullscreen;
734 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
739 static int aglControl( vout_thread_t *p_vout, int i_query, va_list args )
743 case VOUT_SET_VIEWPORT:
745 Rect viewBounds, clipBounds;
746 viewBounds.top = va_arg( args, int);
747 viewBounds.left = va_arg( args, int);
748 viewBounds.bottom = va_arg( args, int);
749 viewBounds.right = va_arg( args, int);
750 clipBounds.top = va_arg( args, int);
751 clipBounds.left = va_arg( args, int);
752 clipBounds.bottom = va_arg( args, int);
753 clipBounds.right = va_arg( args, int);
755 if( !p_vout->b_fullscreen )
758 ** check that the clip rect is not empty, as this is used
759 ** by Firefox to prevent a plugin from displaying during
760 ** a scrolling event. In this case we just prevent buffers
761 ** from being swapped and ignore clipping as this is less
762 ** disruptive than a GL geometry change
765 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
766 || (clipBounds.left == clipBounds.right);
767 if( ! p_vout->p_sys->b_clipped_out )
769 /* ignore consecutive viewport update with identical parameters */
770 if( memcmp(&clipBounds, &(p_vout->p_sys->clipBounds), sizeof(clipBounds) )
771 && memcmp(&viewBounds, &(p_vout->p_sys->viewBounds), sizeof(viewBounds)) )
774 aglSetViewport(p_vout, viewBounds, clipBounds);
775 aglReshape( p_vout );
777 p_vout->p_sys->clipBounds = clipBounds;
778 p_vout->p_sys->viewBounds = viewBounds;
785 case VOUT_REDRAW_RECT:
787 vout_thread_t * p_parent;
790 areaBounds.top = va_arg( args, int);
791 areaBounds.left = va_arg( args, int);
792 areaBounds.bottom = va_arg( args, int);
793 areaBounds.right = va_arg( args, int);
795 /* Ask the opengl module to redraw */
796 p_parent = (vout_thread_t *) p_vout->p_parent;
797 if( p_parent && p_parent->pf_display )
799 p_parent->pf_display( p_parent, NULL );
809 static void aglSwap( vout_thread_t * p_vout )
811 if( ! p_vout->p_sys->b_clipped_out )
813 p_vout->p_sys->b_got_frame = true;
814 aglSwapBuffers(p_vout->p_sys->agl_ctx);
823 /* Enter this function with the p_vout locked */
824 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds )
826 // mozilla plugin provides coordinates based on port bounds
827 // however AGL coordinates are based on window structure region
828 // and are vertically flipped
830 CGrafPtr port = (CGrafPtr)p_vout->p_sys->agl_drawable;
831 Rect winBounds, clientBounds;
833 GetWindowBounds(GetWindowFromPort(port),
834 kWindowStructureRgn, &winBounds);
835 GetWindowBounds(GetWindowFromPort(port),
836 kWindowContentRgn, &clientBounds);
838 /* update video clipping bounds in drawable */
839 rect[0] = (clientBounds.left-winBounds.left)
840 + clipBounds.left; // from window left edge
841 rect[1] = (winBounds.bottom-winBounds.top)
842 - (clientBounds.top-winBounds.top)
843 - clipBounds.bottom; // from window bottom edge
844 rect[2] = clipBounds.right-clipBounds.left; // width
845 rect[3] = clipBounds.bottom-clipBounds.top; // height
847 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT, rect);
848 aglEnable(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT);
850 /* update video internal bounds in drawable */
851 p_vout->p_sys->i_width = viewBounds.right-viewBounds.left;
852 p_vout->p_sys->i_height = viewBounds.bottom-viewBounds.top;
853 p_vout->p_sys->i_offx = -clipBounds.left - viewBounds.left;
854 p_vout->p_sys->i_offy = clipBounds.bottom + viewBounds.top
855 - p_vout->p_sys->i_height;
857 aglUpdateContext(p_vout->p_sys->agl_ctx);
860 //default window event handler
861 static pascal OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData)
863 OSStatus result = noErr;
864 UInt32 class = GetEventClass (event);
865 UInt32 kind = GetEventKind (event);
866 vout_thread_t *p_vout = (vout_thread_t *)userData;
868 result = CallNextEventHandler(nextHandler, event);
869 if(class == kEventClassCommand)
871 HICommand theHICommand;
872 GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL, sizeof( HICommand ), NULL, &theHICommand );
874 switch ( theHICommand.commandID )
877 result = eventNotHandledErr;
880 else if(class == kEventClassWindow)
883 Rect rectPort = {0,0,0,0};
885 GetEventParameter(event, kEventParamDirectObject, typeWindowRef, NULL, sizeof(WindowRef), NULL, &window);
889 GetPortBounds(GetWindowPort(window), &rectPort);
894 case kEventWindowClosed:
895 case kEventWindowZoomed:
896 case kEventWindowBoundsChanged:
900 result = eventNotHandledErr;
903 else if(class == kEventClassMouse)
907 case kEventMouseDown:
911 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
914 case kEventMouseButtonPrimary:
918 var_Get( p_vout, "mouse-button-down", &val );
920 var_Set( p_vout, "mouse-button-down", val );
923 case kEventMouseButtonSecondary:
927 var_Get( p_vout, "mouse-button-down", &val );
929 var_Set( p_vout, "mouse-button-down", val );
932 case kEventMouseButtonTertiary:
936 var_Get( p_vout, "mouse-button-down", &val );
938 var_Set( p_vout, "mouse-button-down", val );
942 result = eventNotHandledErr;
951 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
954 case kEventMouseButtonPrimary:
956 UInt32 clickCount = 0;
957 GetEventParameter(event, kEventParamClickCount, typeUInt32, NULL, sizeof(clickCount), NULL, &clickCount);
963 var_Set((vout_thread_t *) p_vout->p_parent, "fullscreen", val);
969 var_SetBool( p_vout, "mouse-clicked", true );
971 var_Get( p_vout, "mouse-button-down", &val );
973 var_Set( p_vout, "mouse-button-down", val );
977 case kEventMouseButtonSecondary:
981 var_Get( p_vout, "mouse-button-down", &val );
983 var_Set( p_vout, "mouse-button-down", val );
986 case kEventMouseButtonTertiary:
990 var_Get( p_vout, "mouse-button-down", &val );
992 var_Set( p_vout, "mouse-button-down", val );
996 result = eventNotHandledErr;
1001 case kEventMouseMoved:
1006 unsigned int i_x, i_y;
1007 unsigned int i_height = p_vout->p_sys->i_height;
1008 unsigned int i_width = p_vout->p_sys->i_width;
1010 vout_PlacePicture(p_vout, i_width, i_height, &i_x, &i_y, &i_width, &i_height);
1012 GetEventParameter(event, kEventParamWindowMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &ml);
1014 val.i_int = ( ((int)ml.h) - i_x ) *
1015 p_vout->render.i_width / i_width;
1016 var_Set( p_vout, "mouse-x", val );
1018 val.i_int = ( ((int)ml.v) - i_y ) *
1019 p_vout->render.i_height / i_height;
1021 var_Set( p_vout, "mouse-y", val );
1024 var_Set( p_vout, "mouse-moved", val );
1030 result = eventNotHandledErr;
1033 else if(class == kEventClassTextInput)
1037 case kEventTextInputUnicodeForKeyEvent:
1042 result = eventNotHandledErr;
1045 else if(class == kEventClassVLCPlugin)
1049 case kEventVLCPluginShowFullscreen:
1050 ShowWindow (p_vout->p_sys->theWindow);
1051 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
1052 //CGDisplayHideCursor(kCGDirectMainDisplay);
1054 case kEventVLCPluginHideFullscreen:
1055 HideWindow (p_vout->p_sys->theWindow);
1056 SetSystemUIMode( kUIModeNormal, 0);
1057 CGDisplayShowCursor(kCGDirectMainDisplay);
1060 result = eventNotHandledErr;
1067 static int aglLock( vout_thread_t * p_vout )
1069 /* get the underlying CGL context */
1070 CGLContextObj cglContext;
1071 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
1073 if( kCGLNoError == CGLLockContext( cglContext ) )
1075 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
1082 static void aglUnlock( 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 CGLUnlockContext( cglContext );