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 aglDestroyContext(p_vout->p_sys->agl_ctx);
208 else if(VLCIntf && vlc_object_alive (VLCIntf))
210 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
212 /* Close the window */
213 [p_vout->p_sys->o_vout_view performSelectorOnMainThread:@selector(closeVout) withObject:NULL waitUntilDone:YES];
218 free( p_vout->p_sys );
221 static int Init( vout_thread_t * p_vout )
223 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
227 static void End( vout_thread_t * p_vout )
229 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
232 static int Manage( vout_thread_t * p_vout )
234 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
236 [p_vout->p_sys->o_glview reshape];
237 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
239 if( p_vout->i_changes & VOUT_CROP_CHANGE )
241 [p_vout->p_sys->o_glview reshape];
242 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
245 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
247 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
249 p_vout->b_fullscreen = !p_vout->b_fullscreen;
251 if( p_vout->b_fullscreen )
252 [p_vout->p_sys->o_vout_view enterFullscreen];
254 [p_vout->p_sys->o_vout_view leaveFullscreen];
258 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
261 if( p_vout->p_sys->o_vout_view )
262 [p_vout->p_sys->o_vout_view manage];
266 /*****************************************************************************
267 * Control: control facility for the vout
268 *****************************************************************************/
269 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
275 case VOUT_SET_STAY_ON_TOP:
276 b_arg = (bool) va_arg( args, int );
277 [p_vout->p_sys->o_vout_view setOnTop: b_arg];
285 static void Swap( vout_thread_t * p_vout )
287 p_vout->p_sys->b_got_frame = true;
288 [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
291 static int Lock( vout_thread_t * p_vout )
293 if( kCGLNoError == CGLLockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]) )
295 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
301 static void Unlock( vout_thread_t * p_vout )
303 CGLUnlockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]);
306 /*****************************************************************************
307 * VLCGLView implementation
308 *****************************************************************************/
309 @implementation VLCGLView
310 + (void)initVout:(NSValue *)arg
312 vout_thread_t * p_vout = [arg pointerValue];
314 /* Create the GL view */
315 p_vout->p_sys->o_glview = [[VLCGLView alloc] initWithVout: p_vout];
316 [p_vout->p_sys->o_glview autorelease];
318 /* Spawn the window */
319 id old_vout = p_vout->p_sys->o_vout_view;
320 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
321 subView: p_vout->p_sys->o_glview frame: nil] retain];
325 /* This function will reset the o_vout_view. It's useful to go fullscreen. */
326 + (void)resetVout:(NSValue *) voutValue
328 vout_thread_t * p_vout = [voutValue pointerValue];
329 if( p_vout->b_fullscreen )
331 /* Save window size and position */
332 p_vout->p_sys->s_frame.size =
333 [p_vout->p_sys->o_vout_view frame].size;
334 p_vout->p_sys->s_frame.origin =
335 [[p_vout->p_sys->o_vout_view getWindow ]frame].origin;
336 p_vout->p_sys->b_saved_frame = true;
339 [p_vout->p_sys->o_vout_view closeVout];
341 #define o_glview p_vout->p_sys->o_glview
342 o_glview = [[VLCGLView alloc] initWithVout: p_vout];
343 [o_glview autorelease];
345 if( p_vout->p_sys->b_saved_frame )
347 id old_vout = p_vout->p_sys->o_vout_view;
348 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
350 frame: &p_vout->p_sys->s_frame] retain];
355 id old_vout = p_vout->p_sys->o_vout_view;
356 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
357 subView: o_glview frame: nil] retain];
363 - (id) initWithVout: (vout_thread_t *) vout
365 /* Must be called from main thread:
366 * "The NSView class is generally thread-safe, with a few exceptions. You
367 * should create, destroy, resize, move, and perform other operations on NSView
368 * objects only from the main thread of an application. Drawing from secondary
369 * threads is thread-safe as long as you bracket drawing calls with calls to
370 * lockFocusIfCanDraw and unlockFocus." Cocoa Thread Safety */
374 NSOpenGLPixelFormatAttribute attribs[] =
376 NSOpenGLPFADoubleBuffer,
377 NSOpenGLPFAAccelerated,
378 NSOpenGLPFANoRecovery,
379 NSOpenGLPFAColorSize, 24,
380 NSOpenGLPFAAlphaSize, 8,
381 NSOpenGLPFADepthSize, 24,
386 NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
387 initWithAttributes: attribs];
391 msg_Warn( p_vout, "could not create OpenGL video output" );
395 self = [super initWithFrame: NSMakeRect(0,0,10,10) pixelFormat: fmt];
398 [[self openGLContext] makeCurrentContext];
399 [[self openGLContext] update];
401 /* Swap buffers only during the vertical retrace of the monitor.
402 http://developer.apple.com/documentation/GraphicsImaging/
403 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
404 GLint params[] = { 1 };
405 CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval, params );
409 - (BOOL)mouseDownCanMoveWindow
419 NSRect bounds = [self bounds];
421 if( var_GetBool( p_vout, "macosx-stretch" ) )
423 x = bounds.size.width;
424 y = bounds.size.height;
426 else if( bounds.size.height * p_vout->fmt_in.i_visible_width *
427 p_vout->fmt_in.i_sar_num <
428 bounds.size.width * p_vout->fmt_in.i_visible_height *
429 p_vout->fmt_in.i_sar_den )
431 x = ( bounds.size.height * p_vout->fmt_in.i_visible_width *
432 p_vout->fmt_in.i_sar_num ) /
433 ( p_vout->fmt_in.i_visible_height * p_vout->fmt_in.i_sar_den);
435 y = bounds.size.height;
439 x = bounds.size.width;
440 y = ( bounds.size.width * p_vout->fmt_in.i_visible_height *
441 p_vout->fmt_in.i_sar_den) /
442 ( p_vout->fmt_in.i_visible_width * p_vout->fmt_in.i_sar_num );
445 glViewport( ( bounds.size.width - x ) / 2,
446 ( bounds.size.height - y ) / 2, x, y );
450 if( p_vout->p_sys->b_got_frame )
452 /* Ask the opengl module to redraw */
453 vout_thread_t * p_parent;
454 p_parent = (vout_thread_t *) p_vout->p_parent;
456 if( p_parent && p_parent->pf_display )
458 p_parent->pf_display( p_parent, NULL );
463 glClear( GL_COLOR_BUFFER_BIT );
475 - (void) drawRect: (NSRect) rect
478 [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
479 [super drawRect:rect];
485 /*****************************************************************************
486 * embedded AGL context implementation
487 *****************************************************************************/
489 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds );
490 static void aglReshape( vout_thread_t * p_vout );
491 static OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
493 static int aglInit( vout_thread_t * p_vout )
498 p_vout->p_sys->agl_drawable = (AGLDrawable)
499 var_GetInteger( p_vout->p_libvlc, "drawable-agl" );
500 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
502 viewBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-view-top" );
503 viewBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-view-left" );
504 viewBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-view-bottom" );
505 viewBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-view-right" );
506 clipBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-clip-top" );
507 clipBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-clip-left" );
508 clipBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-clip-bottom" );
509 clipBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-clip-right" );
511 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
512 || (clipBounds.left == clipBounds.right);
513 if( ! p_vout->p_sys->b_clipped_out )
516 aglSetViewport(p_vout, viewBounds, clipBounds);
520 p_vout->p_sys->clipBounds = clipBounds;
521 p_vout->p_sys->viewBounds = viewBounds;
526 static void aglEnd( vout_thread_t * p_vout )
528 aglSetCurrentContext(NULL);
529 if( p_vout->p_sys->theWindow ) DisposeWindow( p_vout->p_sys->theWindow );
532 static void aglReshape( vout_thread_t * p_vout )
535 unsigned int i_height = p_vout->p_sys->i_height;
536 unsigned int i_width = p_vout->p_sys->i_width;
538 vout_PlacePicture(p_vout, i_width, i_height, &x, &y, &i_width, &i_height);
540 glViewport( p_vout->p_sys->i_offx + x, p_vout->p_sys->i_offy + y, i_width, i_height );
542 if( p_vout->p_sys->b_got_frame )
544 /* Ask the opengl module to redraw */
545 vout_thread_t * p_parent;
546 p_parent = (vout_thread_t *) p_vout->p_parent;
547 if( p_parent && p_parent->pf_display )
549 p_parent->pf_display( p_parent, NULL );
554 glClear( GL_COLOR_BUFFER_BIT );
558 /* private event class */
561 kEventClassVLCPlugin = 'vlcp',
563 /* private event kinds */
566 kEventVLCPluginShowFullscreen = 32768,
567 kEventVLCPluginHideFullscreen,
570 static void sendEventToMainThread(EventTargetRef target, UInt32 class, UInt32 kind)
573 if( noErr == CreateEvent(NULL, class, kind, 0, kEventAttributeNone, &myEvent) )
575 if( noErr == SetEventParameter(myEvent, kEventParamPostTarget, typeEventTargetRef, sizeof(EventTargetRef), &target) )
577 PostEventToQueue(GetMainEventQueue(), myEvent, kEventPriorityStandard);
579 ReleaseEvent(myEvent);
583 static int aglManage( vout_thread_t * p_vout )
585 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
590 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
592 if( p_vout->i_changes & VOUT_CROP_CHANGE )
597 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
599 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
601 aglSetDrawable(p_vout->p_sys->agl_ctx, NULL);
603 if( p_vout->b_fullscreen )
605 /* Close the fullscreen window and resume normal drawing */
609 p_vout->p_sys->agl_drawable = (AGLDrawable)
610 var_GetInteger( p_vout->p_libvlc, "drawable-agl" );
612 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
614 viewBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-view-top" );
615 viewBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-view-left" );
616 viewBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-view-bottom" );
617 viewBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-view-right" );
618 clipBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-clip-top" );
619 clipBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-clip-left" );
620 clipBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-clip-bottom" );
621 clipBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-clip-right" );
623 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
624 aglSetViewport(p_vout, viewBounds, clipBounds);
626 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty update to the main thread */
627 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow), kEventClassVLCPlugin, kEventVLCPluginHideFullscreen);
633 GDHandle deviceHdl = GetMainDevice();
634 deviceRect = (*deviceHdl)->gdRect;
636 if( !p_vout->p_sys->theWindow )
638 /* Create a window */
639 WindowAttributes windowAttrs;
641 windowAttrs = kWindowStandardDocumentAttributes
642 | kWindowStandardHandlerAttribute
643 | kWindowLiveResizeAttribute
644 | kWindowNoShadowAttribute;
646 windowAttrs &= (~kWindowResizableAttribute);
648 CreateNewWindow(kDocumentWindowClass, windowAttrs, &deviceRect, &p_vout->p_sys->theWindow);
649 if( !p_vout->p_sys->winGroup )
651 CreateWindowGroup(0, &p_vout->p_sys->winGroup);
652 SetWindowGroup(p_vout->p_sys->theWindow, p_vout->p_sys->winGroup);
653 SetWindowGroupParent( p_vout->p_sys->winGroup, GetWindowGroupOfClass(kDocumentWindowClass) ) ;
657 CFStringRef titleKey = CFSTR("Fullscreen VLC media plugin");
658 CFStringRef windowTitle = CFCopyLocalizedString(titleKey, NULL);
659 SetWindowTitleWithCFString(p_vout->p_sys->theWindow, windowTitle);
661 CFRelease(windowTitle);
663 //Install event handler
664 static const EventTypeSpec win_events[] = {
665 { kEventClassMouse, kEventMouseDown },
666 { kEventClassMouse, kEventMouseMoved },
667 { kEventClassMouse, kEventMouseUp },
668 { kEventClassWindow, kEventWindowClosed },
669 { kEventClassWindow, kEventWindowBoundsChanged },
670 { kEventClassCommand, kEventCommandProcess },
671 { kEventClassVLCPlugin, kEventVLCPluginShowFullscreen },
672 { kEventClassVLCPlugin, kEventVLCPluginHideFullscreen },
674 InstallWindowEventHandler (p_vout->p_sys->theWindow, NewEventHandlerUPP (WindowEventHandler), GetEventTypeCount(win_events), win_events, p_vout, NULL);
678 /* just in case device resolution changed */
679 SetWindowBounds(p_vout->p_sys->theWindow, kWindowContentRgn, &deviceRect);
681 glClear( GL_COLOR_BUFFER_BIT );
682 p_vout->p_sys->agl_drawable = (AGLDrawable)GetWindowPort(p_vout->p_sys->theWindow);
683 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
684 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
685 aglSetViewport(p_vout, deviceRect, deviceRect);
686 //aglSetFullScreen(p_vout->p_sys->agl_ctx, device_width, device_height, 0, 0);
688 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty update to the main thread */
689 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow), kEventClassVLCPlugin, kEventVLCPluginShowFullscreen);
693 p_vout->b_fullscreen = !p_vout->b_fullscreen;
694 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
699 static int aglControl( vout_thread_t *p_vout, int i_query, va_list args )
703 case VOUT_SET_VIEWPORT:
705 Rect viewBounds, clipBounds;
706 viewBounds.top = va_arg( args, int);
707 viewBounds.left = va_arg( args, int);
708 viewBounds.bottom = va_arg( args, int);
709 viewBounds.right = va_arg( args, int);
710 clipBounds.top = va_arg( args, int);
711 clipBounds.left = va_arg( args, int);
712 clipBounds.bottom = va_arg( args, int);
713 clipBounds.right = va_arg( args, int);
715 if( !p_vout->b_fullscreen )
718 ** check that the clip rect is not empty, as this is used
719 ** by Firefox to prevent a plugin from displaying during
720 ** a scrolling event. In this case we just prevent buffers
721 ** from being swapped and ignore clipping as this is less
722 ** disruptive than a GL geometry change
725 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
726 || (clipBounds.left == clipBounds.right);
727 if( ! p_vout->p_sys->b_clipped_out )
729 /* ignore consecutive viewport update with identical parameters */
730 if( memcmp(&clipBounds, &(p_vout->p_sys->clipBounds), sizeof(clipBounds) )
731 && memcmp(&viewBounds, &(p_vout->p_sys->viewBounds), sizeof(viewBounds)) )
734 aglSetViewport(p_vout, viewBounds, clipBounds);
735 aglReshape( p_vout );
737 p_vout->p_sys->clipBounds = clipBounds;
738 p_vout->p_sys->viewBounds = viewBounds;
745 case VOUT_REDRAW_RECT:
747 vout_thread_t * p_parent;
750 areaBounds.top = va_arg( args, int);
751 areaBounds.left = va_arg( args, int);
752 areaBounds.bottom = va_arg( args, int);
753 areaBounds.right = va_arg( args, int);
755 /* Ask the opengl module to redraw */
756 p_parent = (vout_thread_t *) p_vout->p_parent;
757 if( p_parent && p_parent->pf_display )
759 p_parent->pf_display( p_parent, NULL );
769 static void aglSwap( vout_thread_t * p_vout )
771 if( ! p_vout->p_sys->b_clipped_out )
773 p_vout->p_sys->b_got_frame = true;
774 aglSwapBuffers(p_vout->p_sys->agl_ctx);
783 /* Enter this function with the p_vout locked */
784 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds )
786 // mozilla plugin provides coordinates based on port bounds
787 // however AGL coordinates are based on window structure region
788 // and are vertically flipped
790 CGrafPtr port = (CGrafPtr)p_vout->p_sys->agl_drawable;
791 Rect winBounds, clientBounds;
793 GetWindowBounds(GetWindowFromPort(port),
794 kWindowStructureRgn, &winBounds);
795 GetWindowBounds(GetWindowFromPort(port),
796 kWindowContentRgn, &clientBounds);
798 /* update video clipping bounds in drawable */
799 rect[0] = (clientBounds.left-winBounds.left)
800 + clipBounds.left; // from window left edge
801 rect[1] = (winBounds.bottom-winBounds.top)
802 - (clientBounds.top-winBounds.top)
803 - clipBounds.bottom; // from window bottom edge
804 rect[2] = clipBounds.right-clipBounds.left; // width
805 rect[3] = clipBounds.bottom-clipBounds.top; // height
807 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT, rect);
808 aglEnable(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT);
810 /* update video internal bounds in drawable */
811 p_vout->p_sys->i_width = viewBounds.right-viewBounds.left;
812 p_vout->p_sys->i_height = viewBounds.bottom-viewBounds.top;
813 p_vout->p_sys->i_offx = -clipBounds.left - viewBounds.left;
814 p_vout->p_sys->i_offy = clipBounds.bottom + viewBounds.top
815 - p_vout->p_sys->i_height;
817 aglUpdateContext(p_vout->p_sys->agl_ctx);
820 //default window event handler
821 static pascal OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData)
823 OSStatus result = noErr;
824 UInt32 class = GetEventClass (event);
825 UInt32 kind = GetEventKind (event);
826 vout_thread_t *p_vout = (vout_thread_t *)userData;
828 result = CallNextEventHandler(nextHandler, event);
829 if(class == kEventClassCommand)
831 HICommand theHICommand;
832 GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL, sizeof( HICommand ), NULL, &theHICommand );
834 switch ( theHICommand.commandID )
837 result = eventNotHandledErr;
840 else if(class == kEventClassWindow)
843 Rect rectPort = {0,0,0,0};
845 GetEventParameter(event, kEventParamDirectObject, typeWindowRef, NULL, sizeof(WindowRef), NULL, &window);
849 GetPortBounds(GetWindowPort(window), &rectPort);
854 case kEventWindowClosed:
855 case kEventWindowZoomed:
856 case kEventWindowBoundsChanged:
860 result = eventNotHandledErr;
863 else if(class == kEventClassMouse)
867 case kEventMouseDown:
871 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
874 case kEventMouseButtonPrimary:
878 var_Get( p_vout, "mouse-button-down", &val );
880 var_Set( p_vout, "mouse-button-down", val );
883 case kEventMouseButtonSecondary:
887 var_Get( p_vout, "mouse-button-down", &val );
889 var_Set( p_vout, "mouse-button-down", val );
892 case kEventMouseButtonTertiary:
896 var_Get( p_vout, "mouse-button-down", &val );
898 var_Set( p_vout, "mouse-button-down", val );
902 result = eventNotHandledErr;
911 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
914 case kEventMouseButtonPrimary:
916 UInt32 clickCount = 0;
917 GetEventParameter(event, kEventParamClickCount, typeUInt32, NULL, sizeof(clickCount), NULL, &clickCount);
923 var_Set((vout_thread_t *) p_vout->p_parent, "fullscreen", val);
929 var_SetBool( p_vout, "mouse-clicked", true );
931 var_Get( p_vout, "mouse-button-down", &val );
933 var_Set( p_vout, "mouse-button-down", val );
937 case kEventMouseButtonSecondary:
941 var_Get( p_vout, "mouse-button-down", &val );
943 var_Set( p_vout, "mouse-button-down", val );
946 case kEventMouseButtonTertiary:
950 var_Get( p_vout, "mouse-button-down", &val );
952 var_Set( p_vout, "mouse-button-down", val );
956 result = eventNotHandledErr;
961 case kEventMouseMoved:
966 unsigned int i_x, i_y;
967 unsigned int i_height = p_vout->p_sys->i_height;
968 unsigned int i_width = p_vout->p_sys->i_width;
970 vout_PlacePicture(p_vout, i_width, i_height, &i_x, &i_y, &i_width, &i_height);
972 GetEventParameter(event, kEventParamWindowMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &ml);
974 val.i_int = ( ((int)ml.h) - i_x ) *
975 p_vout->render.i_width / i_width;
976 var_Set( p_vout, "mouse-x", val );
978 val.i_int = ( ((int)ml.v) - i_y ) *
979 p_vout->render.i_height / i_height;
981 var_Set( p_vout, "mouse-y", val );
984 var_Set( p_vout, "mouse-moved", val );
990 result = eventNotHandledErr;
993 else if(class == kEventClassTextInput)
997 case kEventTextInputUnicodeForKeyEvent:
1002 result = eventNotHandledErr;
1005 else if(class == kEventClassVLCPlugin)
1009 case kEventVLCPluginShowFullscreen:
1010 ShowWindow (p_vout->p_sys->theWindow);
1011 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
1012 //CGDisplayHideCursor(kCGDirectMainDisplay);
1014 case kEventVLCPluginHideFullscreen:
1015 HideWindow (p_vout->p_sys->theWindow);
1016 SetSystemUIMode( kUIModeNormal, 0);
1017 CGDisplayShowCursor(kCGDirectMainDisplay);
1020 result = eventNotHandledErr;
1027 static int aglLock( vout_thread_t * p_vout )
1029 /* get the underlying CGL context */
1030 CGLContextObj cglContext;
1031 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
1033 if( kCGLNoError == CGLLockContext( cglContext ) )
1035 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
1042 static void aglUnlock( vout_thread_t * p_vout )
1044 /* get the underlying CGL context */
1045 CGLContextObj cglContext;
1046 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
1048 CGLUnlockContext( cglContext );