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( p_vout->p_sys->agl_ctx )
219 aglDestroyContext(p_vout->p_sys->agl_ctx);
222 else if(VLCIntf && vlc_object_alive (VLCIntf))
224 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
226 /* Close the window */
227 [p_vout->p_sys->o_vout_view performSelectorOnMainThread:@selector(closeVout) withObject:NULL waitUntilDone:YES];
232 if(VLCIntf && vlc_object_alive (VLCIntf))
234 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
236 /* Close the window */
237 [p_vout->p_sys->o_vout_view performSelectorOnMainThread:@selector(closeVout) withObject:NULL waitUntilDone:YES];
243 free( p_vout->p_sys );
246 static int Init( vout_thread_t * p_vout )
248 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
252 static void End( vout_thread_t * p_vout )
254 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
257 static int Manage( vout_thread_t * p_vout )
259 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
261 [p_vout->p_sys->o_glview reshape];
262 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
264 if( p_vout->i_changes & VOUT_CROP_CHANGE )
266 [p_vout->p_sys->o_glview reshape];
267 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
270 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
272 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
274 p_vout->b_fullscreen = !p_vout->b_fullscreen;
276 if( p_vout->b_fullscreen )
277 [p_vout->p_sys->o_vout_view enterFullscreen];
279 [p_vout->p_sys->o_vout_view leaveFullscreen];
283 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
286 if( p_vout->p_sys->o_vout_view )
287 [p_vout->p_sys->o_vout_view manage];
291 /*****************************************************************************
292 * Control: control facility for the vout
293 *****************************************************************************/
294 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
300 case VOUT_SET_STAY_ON_TOP:
301 b_arg = (bool) va_arg( args, int );
302 [p_vout->p_sys->o_vout_view setOnTop: b_arg];
310 static void Swap( vout_thread_t * p_vout )
312 p_vout->p_sys->b_got_frame = true;
313 [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
316 static int Lock( vout_thread_t * p_vout )
318 if( kCGLNoError == CGLLockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]) )
320 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
326 static void Unlock( vout_thread_t * p_vout )
328 CGLUnlockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]);
331 /*****************************************************************************
332 * VLCGLView implementation
333 *****************************************************************************/
334 @implementation VLCGLView
335 + (void)initVout:(NSValue *)arg
337 vout_thread_t * p_vout = [arg pointerValue];
339 /* Create the GL view */
340 p_vout->p_sys->o_glview = [[VLCGLView alloc] initWithVout: p_vout];
341 [p_vout->p_sys->o_glview autorelease];
343 /* Spawn the window */
344 id old_vout = p_vout->p_sys->o_vout_view;
345 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
346 subView: p_vout->p_sys->o_glview frame: nil] retain];
350 /* This function will reset the o_vout_view. It's useful to go fullscreen. */
351 + (void)resetVout:(NSValue *) voutValue
353 vout_thread_t * p_vout = [voutValue pointerValue];
354 if( p_vout->b_fullscreen )
356 /* Save window size and position */
357 p_vout->p_sys->s_frame.size =
358 [p_vout->p_sys->o_vout_view frame].size;
359 p_vout->p_sys->s_frame.origin =
360 [[p_vout->p_sys->o_vout_view voutWindow]frame].origin;
361 p_vout->p_sys->b_saved_frame = true;
364 [p_vout->p_sys->o_vout_view closeVout];
366 #define o_glview p_vout->p_sys->o_glview
367 o_glview = [[VLCGLView alloc] initWithVout: p_vout];
368 [o_glview autorelease];
370 if( p_vout->p_sys->b_saved_frame )
372 id old_vout = p_vout->p_sys->o_vout_view;
373 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
375 frame: &p_vout->p_sys->s_frame] retain];
380 id old_vout = p_vout->p_sys->o_vout_view;
381 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
382 subView: o_glview frame: nil] retain];
388 - (id) initWithVout: (vout_thread_t *) vout
390 /* Must be called from main thread:
391 * "The NSView class is generally thread-safe, with a few exceptions. You
392 * should create, destroy, resize, move, and perform other operations on NSView
393 * objects only from the main thread of an application. Drawing from secondary
394 * threads is thread-safe as long as you bracket drawing calls with calls to
395 * lockFocusIfCanDraw and unlockFocus." Cocoa Thread Safety */
399 NSOpenGLPixelFormatAttribute attribs[] =
401 NSOpenGLPFADoubleBuffer,
402 NSOpenGLPFAAccelerated,
403 NSOpenGLPFANoRecovery,
404 NSOpenGLPFAColorSize, 24,
405 NSOpenGLPFAAlphaSize, 8,
406 NSOpenGLPFADepthSize, 24,
411 NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
412 initWithAttributes: attribs];
416 msg_Warn( p_vout, "could not create OpenGL video output" );
420 self = [super initWithFrame: NSMakeRect(0,0,10,10) pixelFormat: fmt];
423 [[self openGLContext] makeCurrentContext];
424 [[self openGLContext] update];
426 /* Swap buffers only during the vertical retrace of the monitor.
427 http://developer.apple.com/documentation/GraphicsImaging/
428 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
429 GLint params[] = { 1 };
430 CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval, params );
434 - (BOOL)mouseDownCanMoveWindow
444 NSRect bounds = [self bounds];
446 if( var_GetBool( p_vout, "macosx-stretch" ) )
448 x = bounds.size.width;
449 y = bounds.size.height;
451 else if( bounds.size.height * p_vout->fmt_in.i_visible_width *
452 p_vout->fmt_in.i_sar_num <
453 bounds.size.width * p_vout->fmt_in.i_visible_height *
454 p_vout->fmt_in.i_sar_den )
456 x = ( bounds.size.height * p_vout->fmt_in.i_visible_width *
457 p_vout->fmt_in.i_sar_num ) /
458 ( p_vout->fmt_in.i_visible_height * p_vout->fmt_in.i_sar_den);
460 y = bounds.size.height;
464 x = bounds.size.width;
465 y = ( bounds.size.width * p_vout->fmt_in.i_visible_height *
466 p_vout->fmt_in.i_sar_den) /
467 ( p_vout->fmt_in.i_visible_width * p_vout->fmt_in.i_sar_num );
470 glViewport( ( bounds.size.width - x ) / 2,
471 ( bounds.size.height - y ) / 2, x, y );
475 if( p_vout->p_sys->b_got_frame )
477 /* Ask the opengl module to redraw */
478 vout_thread_t * p_parent;
479 p_parent = (vout_thread_t *) p_vout->p_parent;
481 if( p_parent && p_parent->pf_display )
483 p_parent->pf_display( p_parent, NULL );
488 glClear( GL_COLOR_BUFFER_BIT );
500 - (void) drawRect: (NSRect) rect
503 [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
504 [super drawRect:rect];
510 /*****************************************************************************
511 * embedded AGL context implementation
512 *****************************************************************************/
516 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds );
517 static void aglReshape( vout_thread_t * p_vout );
518 static OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
520 static int aglInit( vout_thread_t * p_vout )
525 p_vout->p_sys->agl_drawable = (AGLDrawable)
526 var_GetInteger( p_vout->p_libvlc, "drawable-agl" );
527 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
529 viewBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-view-top" );
530 viewBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-view-left" );
531 viewBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-view-bottom" );
532 viewBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-view-right" );
533 clipBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-clip-top" );
534 clipBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-clip-left" );
535 clipBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-clip-bottom" );
536 clipBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-clip-right" );
538 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
539 || (clipBounds.left == clipBounds.right);
540 if( ! p_vout->p_sys->b_clipped_out )
543 aglSetViewport(p_vout, viewBounds, clipBounds);
547 p_vout->p_sys->clipBounds = clipBounds;
548 p_vout->p_sys->viewBounds = viewBounds;
553 static void aglEnd( vout_thread_t * p_vout )
555 aglSetCurrentContext(NULL);
556 if( p_vout->p_sys->theWindow )
558 DisposeWindow( p_vout->p_sys->theWindow );
559 p_vout->p_sys->theWindow = NULL;
563 static void aglReshape( vout_thread_t * p_vout )
566 unsigned int i_height = p_vout->p_sys->i_height;
567 unsigned int i_width = p_vout->p_sys->i_width;
569 vout_PlacePicture(p_vout, i_width, i_height, &x, &y, &i_width, &i_height);
571 glViewport( p_vout->p_sys->i_offx + x, p_vout->p_sys->i_offy + y, i_width, i_height );
573 if( p_vout->p_sys->b_got_frame )
575 /* Ask the opengl module to redraw */
576 vout_thread_t * p_parent;
577 p_parent = (vout_thread_t *) p_vout->p_parent;
578 if( p_parent && p_parent->pf_display )
580 p_parent->pf_display( p_parent, NULL );
585 glClear( GL_COLOR_BUFFER_BIT );
589 /* private event class */
592 kEventClassVLCPlugin = 'vlcp',
594 /* private event kinds */
597 kEventVLCPluginShowFullscreen = 32768,
598 kEventVLCPluginHideFullscreen,
601 static void sendEventToMainThread(EventTargetRef target, UInt32 class, UInt32 kind)
604 if( noErr == CreateEvent(NULL, class, kind, 0, kEventAttributeNone, &myEvent) )
606 if( noErr == SetEventParameter(myEvent, kEventParamPostTarget, typeEventTargetRef, sizeof(EventTargetRef), &target) )
608 PostEventToQueue(GetMainEventQueue(), myEvent, kEventPriorityStandard);
610 ReleaseEvent(myEvent);
614 static int aglManage( vout_thread_t * p_vout )
616 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
621 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
623 if( p_vout->i_changes & VOUT_CROP_CHANGE )
628 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
630 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
632 aglSetDrawable(p_vout->p_sys->agl_ctx, NULL);
634 if( p_vout->b_fullscreen )
636 /* Close the fullscreen window and resume normal drawing */
640 p_vout->p_sys->agl_drawable = (AGLDrawable)
641 var_GetInteger( p_vout->p_libvlc, "drawable-agl" );
643 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
645 viewBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-view-top" );
646 viewBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-view-left" );
647 viewBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-view-bottom" );
648 viewBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-view-right" );
649 clipBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-clip-top" );
650 clipBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-clip-left" );
651 clipBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-clip-bottom" );
652 clipBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-clip-right" );
654 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
655 aglSetViewport(p_vout, viewBounds, clipBounds);
657 if( p_vout->p_sys->theWindow )
659 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty
660 * update to the main thread */
661 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow),
662 kEventClassVLCPlugin, kEventVLCPluginHideFullscreen);
667 /* Go into fullscreen */
670 GDHandle deviceHdl = GetMainDevice();
671 deviceRect = (*deviceHdl)->gdRect;
673 if( !p_vout->p_sys->theWindow )
675 /* Create a window */
676 WindowAttributes windowAttrs;
678 windowAttrs = kWindowStandardDocumentAttributes
679 | kWindowStandardHandlerAttribute
680 | kWindowLiveResizeAttribute
681 | kWindowNoShadowAttribute;
683 windowAttrs &= (~kWindowResizableAttribute);
685 CreateNewWindow(kDocumentWindowClass, windowAttrs, &deviceRect, &p_vout->p_sys->theWindow);
686 if( !p_vout->p_sys->winGroup )
688 CreateWindowGroup(0, &p_vout->p_sys->winGroup);
689 SetWindowGroup(p_vout->p_sys->theWindow, p_vout->p_sys->winGroup);
690 SetWindowGroupParent( p_vout->p_sys->winGroup, GetWindowGroupOfClass(kDocumentWindowClass) ) ;
694 CFStringRef titleKey = CFSTR("Fullscreen VLC media plugin");
695 CFStringRef windowTitle = CFCopyLocalizedString(titleKey, NULL);
696 SetWindowTitleWithCFString(p_vout->p_sys->theWindow, windowTitle);
698 CFRelease(windowTitle);
700 //Install event handler
701 static const EventTypeSpec win_events[] = {
702 { kEventClassMouse, kEventMouseDown },
703 { kEventClassMouse, kEventMouseMoved },
704 { kEventClassMouse, kEventMouseUp },
705 { kEventClassWindow, kEventWindowClosed },
706 { kEventClassWindow, kEventWindowBoundsChanged },
707 { kEventClassCommand, kEventCommandProcess },
708 { kEventClassVLCPlugin, kEventVLCPluginShowFullscreen },
709 { kEventClassVLCPlugin, kEventVLCPluginHideFullscreen },
711 InstallWindowEventHandler (p_vout->p_sys->theWindow, NewEventHandlerUPP (WindowEventHandler), GetEventTypeCount(win_events), win_events, p_vout, NULL);
715 /* just in case device resolution changed */
716 SetWindowBounds(p_vout->p_sys->theWindow, kWindowContentRgn, &deviceRect);
718 glClear( GL_COLOR_BUFFER_BIT );
719 p_vout->p_sys->agl_drawable = (AGLDrawable)GetWindowPort(p_vout->p_sys->theWindow);
720 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
721 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
722 aglSetViewport(p_vout, deviceRect, deviceRect);
723 //aglSetFullScreen(p_vout->p_sys->agl_ctx, device_width, device_height, 0, 0);
725 if( p_vout->p_sys->theWindow )
727 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty
728 * update to the main thread */
729 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow),
730 kEventClassVLCPlugin, kEventVLCPluginShowFullscreen);
735 p_vout->b_fullscreen = !p_vout->b_fullscreen;
736 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
741 static int aglControl( vout_thread_t *p_vout, int i_query, va_list args )
745 case VOUT_SET_VIEWPORT:
747 Rect viewBounds, clipBounds;
748 viewBounds.top = va_arg( args, int);
749 viewBounds.left = va_arg( args, int);
750 viewBounds.bottom = va_arg( args, int);
751 viewBounds.right = va_arg( args, int);
752 clipBounds.top = va_arg( args, int);
753 clipBounds.left = va_arg( args, int);
754 clipBounds.bottom = va_arg( args, int);
755 clipBounds.right = va_arg( args, int);
757 if( !p_vout->b_fullscreen )
760 ** check that the clip rect is not empty, as this is used
761 ** by Firefox to prevent a plugin from displaying during
762 ** a scrolling event. In this case we just prevent buffers
763 ** from being swapped and ignore clipping as this is less
764 ** disruptive than a GL geometry change
767 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
768 || (clipBounds.left == clipBounds.right);
769 if( ! p_vout->p_sys->b_clipped_out )
771 /* ignore consecutive viewport update with identical parameters */
772 if( memcmp(&clipBounds, &(p_vout->p_sys->clipBounds), sizeof(clipBounds) )
773 && memcmp(&viewBounds, &(p_vout->p_sys->viewBounds), sizeof(viewBounds)) )
776 aglSetViewport(p_vout, viewBounds, clipBounds);
777 aglReshape( p_vout );
779 p_vout->p_sys->clipBounds = clipBounds;
780 p_vout->p_sys->viewBounds = viewBounds;
787 case VOUT_REDRAW_RECT:
789 vout_thread_t * p_parent;
792 areaBounds.top = va_arg( args, int);
793 areaBounds.left = va_arg( args, int);
794 areaBounds.bottom = va_arg( args, int);
795 areaBounds.right = va_arg( args, int);
797 /* Ask the opengl module to redraw */
798 p_parent = (vout_thread_t *) p_vout->p_parent;
799 if( p_parent && p_parent->pf_display )
801 p_parent->pf_display( p_parent, NULL );
811 static void aglSwap( vout_thread_t * p_vout )
813 if( ! p_vout->p_sys->b_clipped_out )
815 p_vout->p_sys->b_got_frame = true;
816 aglSwapBuffers(p_vout->p_sys->agl_ctx);
825 /* Enter this function with the p_vout locked */
826 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds )
828 // mozilla plugin provides coordinates based on port bounds
829 // however AGL coordinates are based on window structure region
830 // and are vertically flipped
832 CGrafPtr port = (CGrafPtr)p_vout->p_sys->agl_drawable;
833 Rect winBounds, clientBounds;
835 GetWindowBounds(GetWindowFromPort(port),
836 kWindowStructureRgn, &winBounds);
837 GetWindowBounds(GetWindowFromPort(port),
838 kWindowContentRgn, &clientBounds);
840 /* update video clipping bounds in drawable */
841 rect[0] = (clientBounds.left-winBounds.left)
842 + clipBounds.left; // from window left edge
843 rect[1] = (winBounds.bottom-winBounds.top)
844 - (clientBounds.top-winBounds.top)
845 - clipBounds.bottom; // from window bottom edge
846 rect[2] = clipBounds.right-clipBounds.left; // width
847 rect[3] = clipBounds.bottom-clipBounds.top; // height
849 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT, rect);
850 aglEnable(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT);
852 /* update video internal bounds in drawable */
853 p_vout->p_sys->i_width = viewBounds.right-viewBounds.left;
854 p_vout->p_sys->i_height = viewBounds.bottom-viewBounds.top;
855 p_vout->p_sys->i_offx = -clipBounds.left - viewBounds.left;
856 p_vout->p_sys->i_offy = clipBounds.bottom + viewBounds.top
857 - p_vout->p_sys->i_height;
859 aglUpdateContext(p_vout->p_sys->agl_ctx);
862 //default window event handler
863 static pascal OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData)
865 OSStatus result = noErr;
866 UInt32 class = GetEventClass (event);
867 UInt32 kind = GetEventKind (event);
868 vout_thread_t *p_vout = (vout_thread_t *)userData;
870 result = CallNextEventHandler(nextHandler, event);
871 if(class == kEventClassCommand)
873 HICommand theHICommand;
874 GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL, sizeof( HICommand ), NULL, &theHICommand );
876 switch ( theHICommand.commandID )
879 result = eventNotHandledErr;
882 else if(class == kEventClassWindow)
885 Rect rectPort = {0,0,0,0};
887 GetEventParameter(event, kEventParamDirectObject, typeWindowRef, NULL, sizeof(WindowRef), NULL, &window);
891 GetPortBounds(GetWindowPort(window), &rectPort);
896 case kEventWindowClosed:
897 case kEventWindowZoomed:
898 case kEventWindowBoundsChanged:
902 result = eventNotHandledErr;
905 else if(class == kEventClassMouse)
909 case kEventMouseDown:
913 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
916 case kEventMouseButtonPrimary:
920 var_Get( p_vout, "mouse-button-down", &val );
922 var_Set( p_vout, "mouse-button-down", val );
925 case kEventMouseButtonSecondary:
929 var_Get( p_vout, "mouse-button-down", &val );
931 var_Set( p_vout, "mouse-button-down", val );
934 case kEventMouseButtonTertiary:
938 var_Get( p_vout, "mouse-button-down", &val );
940 var_Set( p_vout, "mouse-button-down", val );
944 result = eventNotHandledErr;
953 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
956 case kEventMouseButtonPrimary:
958 UInt32 clickCount = 0;
959 GetEventParameter(event, kEventParamClickCount, typeUInt32, NULL, sizeof(clickCount), NULL, &clickCount);
965 var_Set((vout_thread_t *) p_vout->p_parent, "fullscreen", val);
971 var_SetBool( p_vout, "mouse-clicked", true );
973 var_Get( p_vout, "mouse-button-down", &val );
975 var_Set( p_vout, "mouse-button-down", val );
979 case kEventMouseButtonSecondary:
983 var_Get( p_vout, "mouse-button-down", &val );
985 var_Set( p_vout, "mouse-button-down", val );
988 case kEventMouseButtonTertiary:
992 var_Get( p_vout, "mouse-button-down", &val );
994 var_Set( p_vout, "mouse-button-down", val );
998 result = eventNotHandledErr;
1003 case kEventMouseMoved:
1008 unsigned int i_x, i_y;
1009 unsigned int i_height = p_vout->p_sys->i_height;
1010 unsigned int i_width = p_vout->p_sys->i_width;
1012 vout_PlacePicture(p_vout, i_width, i_height, &i_x, &i_y, &i_width, &i_height);
1014 GetEventParameter(event, kEventParamWindowMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &ml);
1016 val.i_int = ( ((int)ml.h) - i_x ) *
1017 p_vout->render.i_width / i_width;
1018 var_Set( p_vout, "mouse-x", val );
1020 val.i_int = ( ((int)ml.v) - i_y ) *
1021 p_vout->render.i_height / i_height;
1023 var_Set( p_vout, "mouse-y", val );
1026 var_Set( p_vout, "mouse-moved", val );
1032 result = eventNotHandledErr;
1035 else if(class == kEventClassTextInput)
1039 case kEventTextInputUnicodeForKeyEvent:
1044 result = eventNotHandledErr;
1047 else if(class == kEventClassVLCPlugin)
1051 case kEventVLCPluginShowFullscreen:
1052 ShowWindow (p_vout->p_sys->theWindow);
1053 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
1054 //CGDisplayHideCursor(kCGDirectMainDisplay);
1056 case kEventVLCPluginHideFullscreen:
1057 HideWindow (p_vout->p_sys->theWindow);
1058 SetSystemUIMode( kUIModeNormal, 0);
1059 CGDisplayShowCursor(kCGDirectMainDisplay);
1062 result = eventNotHandledErr;
1069 static int aglLock( vout_thread_t * p_vout )
1071 /* get the underlying CGL context */
1072 CGLContextObj cglContext;
1073 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
1075 if( kCGLNoError == CGLLockContext( cglContext ) )
1077 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
1084 static void aglUnlock( vout_thread_t * p_vout )
1086 /* get the underlying CGL context */
1087 CGLContextObj cglContext;
1088 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
1090 CGLUnlockContext( cglContext );