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;
66 /* Mozilla plugin-related variables */
69 AGLDrawable agl_drawable;
71 int i_width, i_height;
73 WindowGroupRef winGroup;
75 Rect clipBounds, viewBounds;
78 /*****************************************************************************
80 *****************************************************************************/
82 static int Init ( vout_thread_t * p_vout );
83 static void End ( vout_thread_t * p_vout );
84 static int Manage ( vout_thread_t * p_vout );
85 static int Control( vout_thread_t *, int, va_list );
86 static void Swap ( vout_thread_t * p_vout );
87 static int Lock ( vout_thread_t * p_vout );
88 static void Unlock ( vout_thread_t * p_vout );
90 static int aglInit ( vout_thread_t * p_vout );
91 static void aglEnd ( vout_thread_t * p_vout );
92 static int aglManage ( vout_thread_t * p_vout );
93 static int aglControl( vout_thread_t *, int, va_list );
94 static void aglSwap ( vout_thread_t * p_vout );
95 static int aglLock ( vout_thread_t * p_vout );
96 static void aglUnlock ( vout_thread_t * p_vout );
98 int OpenVideoGL ( vlc_object_t * p_this )
100 vout_thread_t * p_vout = (vout_thread_t *) p_this;
101 vlc_value_t value_drawable;
103 if( !CGDisplayUsesOpenGLAcceleration( kCGDirectMainDisplay ) )
105 msg_Warn( p_vout, "no OpenGL hardware acceleration found. "
106 "Video display might be slow" );
108 msg_Dbg( p_vout, "display is Quartz Extreme accelerated" );
110 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
111 if( p_vout->p_sys == NULL )
114 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
116 var_Get( p_vout->p_libvlc, "drawable-agl", &value_drawable );
117 if( value_drawable.i_int != 0 )
119 static const GLint ATTRIBUTES[] = {
132 AGLPixelFormat pixFormat;
134 p_vout->p_sys->b_embedded = true;
136 pixFormat = aglChoosePixelFormat(NULL, 0, ATTRIBUTES);
137 if( NULL == pixFormat )
139 msg_Err( p_vout, "no screen renderer available for required attributes." );
140 free( p_vout->p_sys );
144 p_vout->p_sys->agl_ctx = aglCreateContext(pixFormat, NULL);
145 aglDestroyPixelFormat(pixFormat);
146 if( NULL == p_vout->p_sys->agl_ctx )
148 msg_Err( p_vout, "cannot create AGL context." );
149 free( p_vout->p_sys );
154 // tell opengl not to sync buffer swap with vertical retrace (too inefficient)
156 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL, ¶m);
157 aglEnable(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL);
160 p_vout->pf_init = aglInit;
161 p_vout->pf_end = aglEnd;
162 p_vout->pf_manage = aglManage;
163 p_vout->pf_control = aglControl;
164 p_vout->pf_swap = aglSwap;
165 p_vout->pf_lock = aglLock;
166 p_vout->pf_unlock = aglUnlock;
170 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
172 p_vout->p_sys->b_embedded = false;
174 [VLCGLView performSelectorOnMainThread:@selector(initVout:) withObject:[NSValue valueWithPointer:p_vout] waitUntilDone:YES];
178 /* Check to see if initVout: was successfull */
179 if( !p_vout->p_sys->o_vout_view )
181 free( p_vout->p_sys );
185 p_vout->pf_init = Init;
186 p_vout->pf_end = End;
187 p_vout->pf_manage = Manage;
188 p_vout->pf_control= Control;
189 p_vout->pf_swap = Swap;
190 p_vout->pf_lock = Lock;
191 p_vout->pf_unlock = Unlock;
193 p_vout->p_sys->b_got_frame = false;
198 void CloseVideoGL ( vlc_object_t * p_this )
200 vout_thread_t * p_vout = (vout_thread_t *) p_this;
202 msg_Dbg( p_this, "Closing" );
204 if( p_vout->p_sys->b_embedded )
206 if( p_vout->p_sys->agl_ctx )
209 aglDestroyContext(p_vout->p_sys->agl_ctx);
212 else if(VLCIntf && vlc_object_alive (VLCIntf))
214 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
216 /* Close the window */
217 [p_vout->p_sys->o_vout_view performSelectorOnMainThread:@selector(closeVout) withObject:NULL waitUntilDone:YES];
222 free( p_vout->p_sys );
225 static int Init( vout_thread_t * p_vout )
227 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
231 static void End( vout_thread_t * p_vout )
233 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
236 static int Manage( vout_thread_t * p_vout )
238 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
240 [p_vout->p_sys->o_glview reshape];
241 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
243 if( p_vout->i_changes & VOUT_CROP_CHANGE )
245 [p_vout->p_sys->o_glview reshape];
246 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
249 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
251 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
253 p_vout->b_fullscreen = !p_vout->b_fullscreen;
255 if( p_vout->b_fullscreen )
256 [p_vout->p_sys->o_vout_view enterFullscreen];
258 [p_vout->p_sys->o_vout_view leaveFullscreen];
262 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
265 if( p_vout->p_sys->o_vout_view )
266 [p_vout->p_sys->o_vout_view manage];
270 /*****************************************************************************
271 * Control: control facility for the vout
272 *****************************************************************************/
273 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
279 case VOUT_SET_STAY_ON_TOP:
280 b_arg = (bool) va_arg( args, int );
281 [p_vout->p_sys->o_vout_view setOnTop: b_arg];
289 static void Swap( vout_thread_t * p_vout )
291 p_vout->p_sys->b_got_frame = true;
292 [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
295 static int Lock( vout_thread_t * p_vout )
297 if( kCGLNoError == CGLLockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]) )
299 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
305 static void Unlock( vout_thread_t * p_vout )
307 CGLUnlockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]);
310 /*****************************************************************************
311 * VLCGLView implementation
312 *****************************************************************************/
313 @implementation VLCGLView
314 + (void)initVout:(NSValue *)arg
316 vout_thread_t * p_vout = [arg pointerValue];
318 /* Create the GL view */
319 p_vout->p_sys->o_glview = [[VLCGLView alloc] initWithVout: p_vout];
320 [p_vout->p_sys->o_glview autorelease];
322 /* Spawn the window */
323 id old_vout = p_vout->p_sys->o_vout_view;
324 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
325 subView: p_vout->p_sys->o_glview frame: nil] retain];
329 /* This function will reset the o_vout_view. It's useful to go fullscreen. */
330 + (void)resetVout:(NSValue *) voutValue
332 vout_thread_t * p_vout = [voutValue pointerValue];
333 if( p_vout->b_fullscreen )
335 /* Save window size and position */
336 p_vout->p_sys->s_frame.size =
337 [p_vout->p_sys->o_vout_view frame].size;
338 p_vout->p_sys->s_frame.origin =
339 [[p_vout->p_sys->o_vout_view getWindow ]frame].origin;
340 p_vout->p_sys->b_saved_frame = true;
343 [p_vout->p_sys->o_vout_view closeVout];
345 #define o_glview p_vout->p_sys->o_glview
346 o_glview = [[VLCGLView alloc] initWithVout: p_vout];
347 [o_glview autorelease];
349 if( p_vout->p_sys->b_saved_frame )
351 id old_vout = p_vout->p_sys->o_vout_view;
352 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
354 frame: &p_vout->p_sys->s_frame] retain];
359 id old_vout = p_vout->p_sys->o_vout_view;
360 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
361 subView: o_glview frame: nil] retain];
367 - (id) initWithVout: (vout_thread_t *) vout
369 /* Must be called from main thread:
370 * "The NSView class is generally thread-safe, with a few exceptions. You
371 * should create, destroy, resize, move, and perform other operations on NSView
372 * objects only from the main thread of an application. Drawing from secondary
373 * threads is thread-safe as long as you bracket drawing calls with calls to
374 * lockFocusIfCanDraw and unlockFocus." Cocoa Thread Safety */
378 NSOpenGLPixelFormatAttribute attribs[] =
380 NSOpenGLPFADoubleBuffer,
381 NSOpenGLPFAAccelerated,
382 NSOpenGLPFANoRecovery,
383 NSOpenGLPFAColorSize, 24,
384 NSOpenGLPFAAlphaSize, 8,
385 NSOpenGLPFADepthSize, 24,
390 NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
391 initWithAttributes: attribs];
395 msg_Warn( p_vout, "could not create OpenGL video output" );
399 self = [super initWithFrame: NSMakeRect(0,0,10,10) pixelFormat: fmt];
402 [[self openGLContext] makeCurrentContext];
403 [[self openGLContext] update];
405 /* Swap buffers only during the vertical retrace of the monitor.
406 http://developer.apple.com/documentation/GraphicsImaging/
407 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
408 GLint params[] = { 1 };
409 CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval, params );
413 - (BOOL)mouseDownCanMoveWindow
423 NSRect bounds = [self bounds];
425 if( var_GetBool( p_vout, "macosx-stretch" ) )
427 x = bounds.size.width;
428 y = bounds.size.height;
430 else if( bounds.size.height * p_vout->fmt_in.i_visible_width *
431 p_vout->fmt_in.i_sar_num <
432 bounds.size.width * p_vout->fmt_in.i_visible_height *
433 p_vout->fmt_in.i_sar_den )
435 x = ( bounds.size.height * p_vout->fmt_in.i_visible_width *
436 p_vout->fmt_in.i_sar_num ) /
437 ( p_vout->fmt_in.i_visible_height * p_vout->fmt_in.i_sar_den);
439 y = bounds.size.height;
443 x = bounds.size.width;
444 y = ( bounds.size.width * p_vout->fmt_in.i_visible_height *
445 p_vout->fmt_in.i_sar_den) /
446 ( p_vout->fmt_in.i_visible_width * p_vout->fmt_in.i_sar_num );
449 glViewport( ( bounds.size.width - x ) / 2,
450 ( bounds.size.height - y ) / 2, x, y );
454 if( p_vout->p_sys->b_got_frame )
456 /* Ask the opengl module to redraw */
457 vout_thread_t * p_parent;
458 p_parent = (vout_thread_t *) p_vout->p_parent;
460 if( p_parent && p_parent->pf_display )
462 p_parent->pf_display( p_parent, NULL );
467 glClear( GL_COLOR_BUFFER_BIT );
479 - (void) drawRect: (NSRect) rect
482 [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
483 [super drawRect:rect];
489 /*****************************************************************************
490 * embedded AGL context implementation
491 *****************************************************************************/
493 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds );
494 static void aglReshape( vout_thread_t * p_vout );
495 static OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
497 static int aglInit( vout_thread_t * p_vout )
502 p_vout->p_sys->agl_drawable = (AGLDrawable)
503 var_GetInteger( p_vout->p_libvlc, "drawable-agl" );
504 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
506 viewBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-view-top" );
507 viewBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-view-left" );
508 viewBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-view-bottom" );
509 viewBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-view-right" );
510 clipBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-clip-top" );
511 clipBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-clip-left" );
512 clipBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-clip-bottom" );
513 clipBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-clip-right" );
515 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
516 || (clipBounds.left == clipBounds.right);
517 if( ! p_vout->p_sys->b_clipped_out )
520 aglSetViewport(p_vout, viewBounds, clipBounds);
524 p_vout->p_sys->clipBounds = clipBounds;
525 p_vout->p_sys->viewBounds = viewBounds;
530 static void aglEnd( vout_thread_t * p_vout )
532 aglSetCurrentContext(NULL);
533 if( p_vout->p_sys->theWindow )
535 DisposeWindow( p_vout->p_sys->theWindow );
536 p_vout->p_sys->theWindow = NULL;
540 static void aglReshape( vout_thread_t * p_vout )
543 unsigned int i_height = p_vout->p_sys->i_height;
544 unsigned int i_width = p_vout->p_sys->i_width;
546 vout_PlacePicture(p_vout, i_width, i_height, &x, &y, &i_width, &i_height);
548 glViewport( p_vout->p_sys->i_offx + x, p_vout->p_sys->i_offy + y, i_width, i_height );
550 if( p_vout->p_sys->b_got_frame )
552 /* Ask the opengl module to redraw */
553 vout_thread_t * p_parent;
554 p_parent = (vout_thread_t *) p_vout->p_parent;
555 if( p_parent && p_parent->pf_display )
557 p_parent->pf_display( p_parent, NULL );
562 glClear( GL_COLOR_BUFFER_BIT );
566 /* private event class */
569 kEventClassVLCPlugin = 'vlcp',
571 /* private event kinds */
574 kEventVLCPluginShowFullscreen = 32768,
575 kEventVLCPluginHideFullscreen,
578 static void sendEventToMainThread(EventTargetRef target, UInt32 class, UInt32 kind)
581 if( noErr == CreateEvent(NULL, class, kind, 0, kEventAttributeNone, &myEvent) )
583 if( noErr == SetEventParameter(myEvent, kEventParamPostTarget, typeEventTargetRef, sizeof(EventTargetRef), &target) )
585 PostEventToQueue(GetMainEventQueue(), myEvent, kEventPriorityStandard);
587 ReleaseEvent(myEvent);
591 static int aglManage( vout_thread_t * p_vout )
593 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
598 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
600 if( p_vout->i_changes & VOUT_CROP_CHANGE )
605 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
607 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
609 aglSetDrawable(p_vout->p_sys->agl_ctx, NULL);
611 if( p_vout->b_fullscreen )
613 /* Close the fullscreen window and resume normal drawing */
617 p_vout->p_sys->agl_drawable = (AGLDrawable)
618 var_GetInteger( p_vout->p_libvlc, "drawable-agl" );
620 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
622 viewBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-view-top" );
623 viewBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-view-left" );
624 viewBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-view-bottom" );
625 viewBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-view-right" );
626 clipBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-clip-top" );
627 clipBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-clip-left" );
628 clipBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-clip-bottom" );
629 clipBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-clip-right" );
631 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
632 aglSetViewport(p_vout, viewBounds, clipBounds);
634 if( p_vout->p_sys->theWindow )
636 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty
637 * update to the main thread */
638 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow),
639 kEventClassVLCPlugin, kEventVLCPluginHideFullscreen);
644 /* Go into fullscreen */
647 GDHandle deviceHdl = GetMainDevice();
648 deviceRect = (*deviceHdl)->gdRect;
650 if( !p_vout->p_sys->theWindow )
652 /* Create a window */
653 WindowAttributes windowAttrs;
655 windowAttrs = kWindowStandardDocumentAttributes
656 | kWindowStandardHandlerAttribute
657 | kWindowLiveResizeAttribute
658 | kWindowNoShadowAttribute;
660 windowAttrs &= (~kWindowResizableAttribute);
662 CreateNewWindow(kDocumentWindowClass, windowAttrs, &deviceRect, &p_vout->p_sys->theWindow);
663 if( !p_vout->p_sys->winGroup )
665 CreateWindowGroup(0, &p_vout->p_sys->winGroup);
666 SetWindowGroup(p_vout->p_sys->theWindow, p_vout->p_sys->winGroup);
667 SetWindowGroupParent( p_vout->p_sys->winGroup, GetWindowGroupOfClass(kDocumentWindowClass) ) ;
671 CFStringRef titleKey = CFSTR("Fullscreen VLC media plugin");
672 CFStringRef windowTitle = CFCopyLocalizedString(titleKey, NULL);
673 SetWindowTitleWithCFString(p_vout->p_sys->theWindow, windowTitle);
675 CFRelease(windowTitle);
677 //Install event handler
678 static const EventTypeSpec win_events[] = {
679 { kEventClassMouse, kEventMouseDown },
680 { kEventClassMouse, kEventMouseMoved },
681 { kEventClassMouse, kEventMouseUp },
682 { kEventClassWindow, kEventWindowClosed },
683 { kEventClassWindow, kEventWindowBoundsChanged },
684 { kEventClassCommand, kEventCommandProcess },
685 { kEventClassVLCPlugin, kEventVLCPluginShowFullscreen },
686 { kEventClassVLCPlugin, kEventVLCPluginHideFullscreen },
688 InstallWindowEventHandler (p_vout->p_sys->theWindow, NewEventHandlerUPP (WindowEventHandler), GetEventTypeCount(win_events), win_events, p_vout, NULL);
692 /* just in case device resolution changed */
693 SetWindowBounds(p_vout->p_sys->theWindow, kWindowContentRgn, &deviceRect);
695 glClear( GL_COLOR_BUFFER_BIT );
696 p_vout->p_sys->agl_drawable = (AGLDrawable)GetWindowPort(p_vout->p_sys->theWindow);
697 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
698 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
699 aglSetViewport(p_vout, deviceRect, deviceRect);
700 //aglSetFullScreen(p_vout->p_sys->agl_ctx, device_width, device_height, 0, 0);
702 if( p_vout->p_sys->theWindow )
704 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty
705 * update to the main thread */
706 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow),
707 kEventClassVLCPlugin, kEventVLCPluginShowFullscreen);
712 p_vout->b_fullscreen = !p_vout->b_fullscreen;
713 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
718 static int aglControl( vout_thread_t *p_vout, int i_query, va_list args )
722 case VOUT_SET_VIEWPORT:
724 Rect viewBounds, clipBounds;
725 viewBounds.top = va_arg( args, int);
726 viewBounds.left = va_arg( args, int);
727 viewBounds.bottom = va_arg( args, int);
728 viewBounds.right = va_arg( args, int);
729 clipBounds.top = va_arg( args, int);
730 clipBounds.left = va_arg( args, int);
731 clipBounds.bottom = va_arg( args, int);
732 clipBounds.right = va_arg( args, int);
734 if( !p_vout->b_fullscreen )
737 ** check that the clip rect is not empty, as this is used
738 ** by Firefox to prevent a plugin from displaying during
739 ** a scrolling event. In this case we just prevent buffers
740 ** from being swapped and ignore clipping as this is less
741 ** disruptive than a GL geometry change
744 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
745 || (clipBounds.left == clipBounds.right);
746 if( ! p_vout->p_sys->b_clipped_out )
748 /* ignore consecutive viewport update with identical parameters */
749 if( memcmp(&clipBounds, &(p_vout->p_sys->clipBounds), sizeof(clipBounds) )
750 && memcmp(&viewBounds, &(p_vout->p_sys->viewBounds), sizeof(viewBounds)) )
753 aglSetViewport(p_vout, viewBounds, clipBounds);
754 aglReshape( p_vout );
756 p_vout->p_sys->clipBounds = clipBounds;
757 p_vout->p_sys->viewBounds = viewBounds;
764 case VOUT_REDRAW_RECT:
766 vout_thread_t * p_parent;
769 areaBounds.top = va_arg( args, int);
770 areaBounds.left = va_arg( args, int);
771 areaBounds.bottom = va_arg( args, int);
772 areaBounds.right = va_arg( args, int);
774 /* Ask the opengl module to redraw */
775 p_parent = (vout_thread_t *) p_vout->p_parent;
776 if( p_parent && p_parent->pf_display )
778 p_parent->pf_display( p_parent, NULL );
788 static void aglSwap( vout_thread_t * p_vout )
790 if( ! p_vout->p_sys->b_clipped_out )
792 p_vout->p_sys->b_got_frame = true;
793 aglSwapBuffers(p_vout->p_sys->agl_ctx);
802 /* Enter this function with the p_vout locked */
803 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds )
805 // mozilla plugin provides coordinates based on port bounds
806 // however AGL coordinates are based on window structure region
807 // and are vertically flipped
809 CGrafPtr port = (CGrafPtr)p_vout->p_sys->agl_drawable;
810 Rect winBounds, clientBounds;
812 GetWindowBounds(GetWindowFromPort(port),
813 kWindowStructureRgn, &winBounds);
814 GetWindowBounds(GetWindowFromPort(port),
815 kWindowContentRgn, &clientBounds);
817 /* update video clipping bounds in drawable */
818 rect[0] = (clientBounds.left-winBounds.left)
819 + clipBounds.left; // from window left edge
820 rect[1] = (winBounds.bottom-winBounds.top)
821 - (clientBounds.top-winBounds.top)
822 - clipBounds.bottom; // from window bottom edge
823 rect[2] = clipBounds.right-clipBounds.left; // width
824 rect[3] = clipBounds.bottom-clipBounds.top; // height
826 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT, rect);
827 aglEnable(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT);
829 /* update video internal bounds in drawable */
830 p_vout->p_sys->i_width = viewBounds.right-viewBounds.left;
831 p_vout->p_sys->i_height = viewBounds.bottom-viewBounds.top;
832 p_vout->p_sys->i_offx = -clipBounds.left - viewBounds.left;
833 p_vout->p_sys->i_offy = clipBounds.bottom + viewBounds.top
834 - p_vout->p_sys->i_height;
836 aglUpdateContext(p_vout->p_sys->agl_ctx);
839 //default window event handler
840 static pascal OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData)
842 OSStatus result = noErr;
843 UInt32 class = GetEventClass (event);
844 UInt32 kind = GetEventKind (event);
845 vout_thread_t *p_vout = (vout_thread_t *)userData;
847 result = CallNextEventHandler(nextHandler, event);
848 if(class == kEventClassCommand)
850 HICommand theHICommand;
851 GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL, sizeof( HICommand ), NULL, &theHICommand );
853 switch ( theHICommand.commandID )
856 result = eventNotHandledErr;
859 else if(class == kEventClassWindow)
862 Rect rectPort = {0,0,0,0};
864 GetEventParameter(event, kEventParamDirectObject, typeWindowRef, NULL, sizeof(WindowRef), NULL, &window);
868 GetPortBounds(GetWindowPort(window), &rectPort);
873 case kEventWindowClosed:
874 case kEventWindowZoomed:
875 case kEventWindowBoundsChanged:
879 result = eventNotHandledErr;
882 else if(class == kEventClassMouse)
886 case kEventMouseDown:
890 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
893 case kEventMouseButtonPrimary:
897 var_Get( p_vout, "mouse-button-down", &val );
899 var_Set( p_vout, "mouse-button-down", val );
902 case kEventMouseButtonSecondary:
906 var_Get( p_vout, "mouse-button-down", &val );
908 var_Set( p_vout, "mouse-button-down", val );
911 case kEventMouseButtonTertiary:
915 var_Get( p_vout, "mouse-button-down", &val );
917 var_Set( p_vout, "mouse-button-down", val );
921 result = eventNotHandledErr;
930 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
933 case kEventMouseButtonPrimary:
935 UInt32 clickCount = 0;
936 GetEventParameter(event, kEventParamClickCount, typeUInt32, NULL, sizeof(clickCount), NULL, &clickCount);
942 var_Set((vout_thread_t *) p_vout->p_parent, "fullscreen", val);
948 var_SetBool( p_vout, "mouse-clicked", true );
950 var_Get( p_vout, "mouse-button-down", &val );
952 var_Set( p_vout, "mouse-button-down", val );
956 case kEventMouseButtonSecondary:
960 var_Get( p_vout, "mouse-button-down", &val );
962 var_Set( p_vout, "mouse-button-down", val );
965 case kEventMouseButtonTertiary:
969 var_Get( p_vout, "mouse-button-down", &val );
971 var_Set( p_vout, "mouse-button-down", val );
975 result = eventNotHandledErr;
980 case kEventMouseMoved:
985 unsigned int i_x, i_y;
986 unsigned int i_height = p_vout->p_sys->i_height;
987 unsigned int i_width = p_vout->p_sys->i_width;
989 vout_PlacePicture(p_vout, i_width, i_height, &i_x, &i_y, &i_width, &i_height);
991 GetEventParameter(event, kEventParamWindowMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &ml);
993 val.i_int = ( ((int)ml.h) - i_x ) *
994 p_vout->render.i_width / i_width;
995 var_Set( p_vout, "mouse-x", val );
997 val.i_int = ( ((int)ml.v) - i_y ) *
998 p_vout->render.i_height / i_height;
1000 var_Set( p_vout, "mouse-y", val );
1003 var_Set( p_vout, "mouse-moved", val );
1009 result = eventNotHandledErr;
1012 else if(class == kEventClassTextInput)
1016 case kEventTextInputUnicodeForKeyEvent:
1021 result = eventNotHandledErr;
1024 else if(class == kEventClassVLCPlugin)
1028 case kEventVLCPluginShowFullscreen:
1029 ShowWindow (p_vout->p_sys->theWindow);
1030 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
1031 //CGDisplayHideCursor(kCGDirectMainDisplay);
1033 case kEventVLCPluginHideFullscreen:
1034 HideWindow (p_vout->p_sys->theWindow);
1035 SetSystemUIMode( kUIModeNormal, 0);
1036 CGDisplayShowCursor(kCGDirectMainDisplay);
1039 result = eventNotHandledErr;
1046 static int aglLock( vout_thread_t * p_vout )
1048 /* get the underlying CGL context */
1049 CGLContextObj cglContext;
1050 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
1052 if( kCGLNoError == CGLLockContext( cglContext ) )
1054 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
1061 static void aglUnlock( vout_thread_t * p_vout )
1063 /* get the underlying CGL context */
1064 CGLContextObj cglContext;
1065 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
1067 CGLUnlockContext( cglContext );