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 <stdlib.h> /* free() */
36 #include <vlc_common.h>
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;
107 if( !CGDisplayUsesOpenGLAcceleration( kCGDirectMainDisplay ) )
109 msg_Warn( p_vout, "no OpenGL hardware acceleration found. "
110 "Video display might be slow" );
112 msg_Dbg( p_vout, "display is Quartz Extreme accelerated" );
114 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
115 if( p_vout->p_sys == NULL )
118 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
122 i_drawable_agl = var_GetInteger( p_vout->p_libvlc, "drawable-agl" );
123 if( i_drawable_agl > 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;
213 if( p_vout->p_sys->b_embedded )
215 /* If the fullscreen window is still open, close it */
216 if( p_vout->b_fullscreen )
218 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
220 var_SetBool( p_vout->p_parent, "fullscreen", false );
222 if( p_vout->p_sys->agl_ctx )
225 aglDestroyContext(p_vout->p_sys->agl_ctx);
230 if(VLCIntf && vlc_object_alive (VLCIntf))
232 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
234 /* Close the window */
235 [p_vout->p_sys->o_vout_view performSelectorOnMainThread:@selector(closeVout) withObject:NULL waitUntilDone:YES];
240 free( p_vout->p_sys );
243 static int Init( vout_thread_t * p_vout )
245 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
249 static void End( vout_thread_t * p_vout )
251 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
254 static int Manage( vout_thread_t * p_vout )
256 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
258 [p_vout->p_sys->o_glview reshape];
259 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
261 if( p_vout->i_changes & VOUT_CROP_CHANGE )
263 [p_vout->p_sys->o_glview reshape];
264 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
267 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
269 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
271 p_vout->b_fullscreen = !p_vout->b_fullscreen;
273 if( p_vout->b_fullscreen )
274 [p_vout->p_sys->o_vout_view enterFullscreen];
276 [p_vout->p_sys->o_vout_view leaveFullscreen];
280 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
283 if( p_vout->p_sys->o_vout_view )
284 [p_vout->p_sys->o_vout_view manage];
288 /*****************************************************************************
289 * Control: control facility for the vout
290 *****************************************************************************/
291 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
297 case VOUT_SET_STAY_ON_TOP:
298 b_arg = (bool) va_arg( args, int );
299 [p_vout->p_sys->o_vout_view setOnTop: b_arg];
307 static void Swap( vout_thread_t * p_vout )
309 p_vout->p_sys->b_got_frame = true;
310 [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
313 static int Lock( vout_thread_t * p_vout )
315 if( kCGLNoError == CGLLockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]) )
317 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
323 static void Unlock( vout_thread_t * p_vout )
325 CGLUnlockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]);
328 /*****************************************************************************
329 * VLCGLView implementation
330 *****************************************************************************/
331 @implementation VLCGLView
332 + (void)initVout:(NSValue *)arg
334 vout_thread_t * p_vout = [arg pointerValue];
336 /* Create the GL view */
337 p_vout->p_sys->o_glview = [[VLCGLView alloc] initWithVout: p_vout];
338 [p_vout->p_sys->o_glview autorelease];
340 /* Spawn the window */
341 id old_vout = p_vout->p_sys->o_vout_view;
342 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
343 subView: p_vout->p_sys->o_glview frame: nil] retain];
347 /* This function will reset the o_vout_view. It's useful to go fullscreen. */
348 + (void)resetVout:(NSValue *) voutValue
350 vout_thread_t * p_vout = [voutValue pointerValue];
351 if( p_vout->b_fullscreen )
353 /* Save window size and position */
354 p_vout->p_sys->s_frame.size =
355 [p_vout->p_sys->o_vout_view frame].size;
356 p_vout->p_sys->s_frame.origin =
357 [[p_vout->p_sys->o_vout_view voutWindow]frame].origin;
358 p_vout->p_sys->b_saved_frame = true;
361 [p_vout->p_sys->o_vout_view closeVout];
363 #define o_glview p_vout->p_sys->o_glview
364 o_glview = [[VLCGLView alloc] initWithVout: p_vout];
365 [o_glview autorelease];
367 if( p_vout->p_sys->b_saved_frame )
369 id old_vout = p_vout->p_sys->o_vout_view;
370 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
372 frame: &p_vout->p_sys->s_frame] retain];
377 id old_vout = p_vout->p_sys->o_vout_view;
378 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
379 subView: o_glview frame: nil] retain];
385 - (id) initWithVout: (vout_thread_t *) vout
387 /* Must be called from main thread:
388 * "The NSView class is generally thread-safe, with a few exceptions. You
389 * should create, destroy, resize, move, and perform other operations on NSView
390 * objects only from the main thread of an application. Drawing from secondary
391 * threads is thread-safe as long as you bracket drawing calls with calls to
392 * lockFocusIfCanDraw and unlockFocus." Cocoa Thread Safety */
396 NSOpenGLPixelFormatAttribute attribs[] =
398 NSOpenGLPFADoubleBuffer,
399 NSOpenGLPFAAccelerated,
400 NSOpenGLPFANoRecovery,
401 NSOpenGLPFAColorSize, 24,
402 NSOpenGLPFAAlphaSize, 8,
403 NSOpenGLPFADepthSize, 24,
408 NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
409 initWithAttributes: attribs];
413 msg_Warn( p_vout, "could not create OpenGL video output" );
417 self = [super initWithFrame: NSMakeRect(0,0,10,10) pixelFormat: fmt];
420 [[self openGLContext] makeCurrentContext];
421 [[self openGLContext] update];
423 /* Swap buffers only during the vertical retrace of the monitor.
424 http://developer.apple.com/documentation/GraphicsImaging/
425 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
426 GLint params[] = { 1 };
427 CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval, params );
431 - (BOOL)mouseDownCanMoveWindow
441 NSRect bounds = [self bounds];
443 if( var_GetBool( p_vout, "macosx-stretch" ) )
445 x = bounds.size.width;
446 y = bounds.size.height;
448 else if( bounds.size.height * p_vout->fmt_in.i_visible_width *
449 p_vout->fmt_in.i_sar_num <
450 bounds.size.width * p_vout->fmt_in.i_visible_height *
451 p_vout->fmt_in.i_sar_den )
453 x = ( bounds.size.height * p_vout->fmt_in.i_visible_width *
454 p_vout->fmt_in.i_sar_num ) /
455 ( p_vout->fmt_in.i_visible_height * p_vout->fmt_in.i_sar_den);
457 y = bounds.size.height;
461 x = bounds.size.width;
462 y = ( bounds.size.width * p_vout->fmt_in.i_visible_height *
463 p_vout->fmt_in.i_sar_den) /
464 ( p_vout->fmt_in.i_visible_width * p_vout->fmt_in.i_sar_num );
467 glViewport( ( bounds.size.width - x ) / 2,
468 ( bounds.size.height - y ) / 2, x, y );
472 if( p_vout->p_sys->b_got_frame )
474 /* Ask the opengl module to redraw */
475 vout_thread_t * p_parent;
476 p_parent = (vout_thread_t *) p_vout->p_parent;
478 if( p_parent && p_parent->pf_display )
480 p_parent->pf_display( p_parent, NULL );
485 glClear( GL_COLOR_BUFFER_BIT );
497 - (void) drawRect: (NSRect) rect
500 [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
501 [super drawRect:rect];
507 NSWindow *window = [self window];
509 if ([window respondsToSelector:@selector(disableScreenUpdatesUntilFlush)])
511 [window disableScreenUpdatesUntilFlush];
519 /*****************************************************************************
520 * embedded AGL context implementation
521 *****************************************************************************/
525 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds );
526 static void aglReshape( vout_thread_t * p_vout );
527 static OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
529 static int aglInit( vout_thread_t * p_vout )
534 p_vout->p_sys->agl_drawable = (AGLDrawable)
535 var_GetInteger( p_vout->p_libvlc, "drawable-agl" );
536 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
538 viewBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-view-top" );
539 viewBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-view-left" );
540 viewBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-view-bottom" );
541 viewBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-view-right" );
542 clipBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-clip-top" );
543 clipBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-clip-left" );
544 clipBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-clip-bottom" );
545 clipBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-clip-right" );
547 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
548 || (clipBounds.left == clipBounds.right);
549 if( ! p_vout->p_sys->b_clipped_out )
552 aglSetViewport(p_vout, viewBounds, clipBounds);
556 p_vout->p_sys->clipBounds = clipBounds;
557 p_vout->p_sys->viewBounds = viewBounds;
562 static void aglEnd( vout_thread_t * p_vout )
564 aglSetCurrentContext(NULL);
565 if( p_vout->p_sys->theWindow )
567 DisposeWindow( p_vout->p_sys->theWindow );
568 p_vout->p_sys->theWindow = NULL;
572 static void aglReshape( vout_thread_t * p_vout )
575 unsigned int i_height = p_vout->p_sys->i_height;
576 unsigned int i_width = p_vout->p_sys->i_width;
578 vout_PlacePicture(p_vout, i_width, i_height, &x, &y, &i_width, &i_height);
580 glViewport( p_vout->p_sys->i_offx + x, p_vout->p_sys->i_offy + y, i_width, i_height );
582 if( p_vout->p_sys->b_got_frame )
584 /* Ask the opengl module to redraw */
585 vout_thread_t * p_parent;
586 p_parent = (vout_thread_t *) p_vout->p_parent;
587 if( p_parent && p_parent->pf_display )
589 p_parent->pf_display( p_parent, NULL );
594 glClear( GL_COLOR_BUFFER_BIT );
598 /* private event class */
601 kEventClassVLCPlugin = 'vlcp',
603 /* private event kinds */
606 kEventVLCPluginShowFullscreen = 32768,
607 kEventVLCPluginHideFullscreen,
610 static void sendEventToMainThread(EventTargetRef target, UInt32 class, UInt32 kind)
613 if( noErr == CreateEvent(NULL, class, kind, 0, kEventAttributeNone, &myEvent) )
615 if( noErr == SetEventParameter(myEvent, kEventParamPostTarget, typeEventTargetRef, sizeof(EventTargetRef), &target) )
617 PostEventToQueue(GetMainEventQueue(), myEvent, kEventPriorityStandard);
619 ReleaseEvent(myEvent);
623 static int aglManage( vout_thread_t * p_vout )
625 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
630 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
632 if( p_vout->i_changes & VOUT_CROP_CHANGE )
637 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
639 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
641 aglSetDrawable(p_vout->p_sys->agl_ctx, NULL);
643 if( p_vout->b_fullscreen )
645 /* Close the fullscreen window and resume normal drawing */
649 p_vout->p_sys->agl_drawable = (AGLDrawable)
650 var_GetInteger( p_vout->p_libvlc, "drawable-agl" );
652 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
654 viewBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-view-top" );
655 viewBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-view-left" );
656 viewBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-view-bottom" );
657 viewBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-view-right" );
658 clipBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-clip-top" );
659 clipBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-clip-left" );
660 clipBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-clip-bottom" );
661 clipBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-clip-right" );
663 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
664 aglSetViewport(p_vout, viewBounds, clipBounds);
666 if( p_vout->p_sys->theWindow )
668 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty
669 * update to the main thread */
670 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow),
671 kEventClassVLCPlugin, kEventVLCPluginHideFullscreen);
676 /* Go into fullscreen */
679 GDHandle deviceHdl = GetMainDevice();
680 deviceRect = (*deviceHdl)->gdRect;
682 if( !p_vout->p_sys->theWindow )
684 /* Create a window */
685 WindowAttributes windowAttrs;
687 windowAttrs = kWindowStandardDocumentAttributes
688 | kWindowStandardHandlerAttribute
689 | kWindowLiveResizeAttribute
690 | kWindowNoShadowAttribute;
692 windowAttrs &= (~kWindowResizableAttribute);
694 CreateNewWindow(kDocumentWindowClass, windowAttrs, &deviceRect, &p_vout->p_sys->theWindow);
695 if( !p_vout->p_sys->winGroup )
697 CreateWindowGroup(0, &p_vout->p_sys->winGroup);
698 SetWindowGroup(p_vout->p_sys->theWindow, p_vout->p_sys->winGroup);
699 SetWindowGroupParent( p_vout->p_sys->winGroup, GetWindowGroupOfClass(kDocumentWindowClass) ) ;
703 CFStringRef titleKey = CFSTR("Fullscreen VLC media plugin");
704 CFStringRef windowTitle = CFCopyLocalizedString(titleKey, NULL);
705 SetWindowTitleWithCFString(p_vout->p_sys->theWindow, windowTitle);
707 CFRelease(windowTitle);
709 //Install event handler
710 static const EventTypeSpec win_events[] = {
711 { kEventClassMouse, kEventMouseDown },
712 { kEventClassMouse, kEventMouseMoved },
713 { kEventClassMouse, kEventMouseUp },
714 { kEventClassWindow, kEventWindowClosed },
715 { kEventClassWindow, kEventWindowBoundsChanged },
716 { kEventClassCommand, kEventCommandProcess },
717 { kEventClassVLCPlugin, kEventVLCPluginShowFullscreen },
718 { kEventClassVLCPlugin, kEventVLCPluginHideFullscreen },
720 InstallWindowEventHandler (p_vout->p_sys->theWindow, NewEventHandlerUPP (WindowEventHandler), GetEventTypeCount(win_events), win_events, p_vout, NULL);
724 /* just in case device resolution changed */
725 SetWindowBounds(p_vout->p_sys->theWindow, kWindowContentRgn, &deviceRect);
727 glClear( GL_COLOR_BUFFER_BIT );
728 p_vout->p_sys->agl_drawable = (AGLDrawable)GetWindowPort(p_vout->p_sys->theWindow);
729 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
730 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
731 aglSetViewport(p_vout, deviceRect, deviceRect);
732 //aglSetFullScreen(p_vout->p_sys->agl_ctx, device_width, device_height, 0, 0);
734 if( p_vout->p_sys->theWindow )
736 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty
737 * update to the main thread */
738 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow),
739 kEventClassVLCPlugin, kEventVLCPluginShowFullscreen);
744 p_vout->b_fullscreen = !p_vout->b_fullscreen;
745 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
750 static int aglControl( vout_thread_t *p_vout, int i_query, va_list args )
754 case VOUT_SET_VIEWPORT:
756 Rect viewBounds, clipBounds;
757 viewBounds.top = va_arg( args, int);
758 viewBounds.left = va_arg( args, int);
759 viewBounds.bottom = va_arg( args, int);
760 viewBounds.right = va_arg( args, int);
761 clipBounds.top = va_arg( args, int);
762 clipBounds.left = va_arg( args, int);
763 clipBounds.bottom = va_arg( args, int);
764 clipBounds.right = va_arg( args, int);
766 if( !p_vout->b_fullscreen )
769 ** check that the clip rect is not empty, as this is used
770 ** by Firefox to prevent a plugin from displaying during
771 ** a scrolling event. In this case we just prevent buffers
772 ** from being swapped and ignore clipping as this is less
773 ** disruptive than a GL geometry change
776 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
777 || (clipBounds.left == clipBounds.right);
778 if( ! p_vout->p_sys->b_clipped_out )
780 /* ignore consecutive viewport update with identical parameters */
781 if( memcmp(&clipBounds, &(p_vout->p_sys->clipBounds), sizeof(clipBounds) )
782 && memcmp(&viewBounds, &(p_vout->p_sys->viewBounds), sizeof(viewBounds)) )
785 aglSetViewport(p_vout, viewBounds, clipBounds);
786 aglReshape( p_vout );
788 p_vout->p_sys->clipBounds = clipBounds;
789 p_vout->p_sys->viewBounds = viewBounds;
796 case VOUT_REDRAW_RECT:
798 vout_thread_t * p_parent;
801 areaBounds.top = va_arg( args, int);
802 areaBounds.left = va_arg( args, int);
803 areaBounds.bottom = va_arg( args, int);
804 areaBounds.right = va_arg( args, int);
806 /* Ask the opengl module to redraw */
807 p_parent = (vout_thread_t *) p_vout->p_parent;
808 if( p_parent && p_parent->pf_display )
810 p_parent->pf_display( p_parent, NULL );
820 static void aglSwap( vout_thread_t * p_vout )
822 if( ! p_vout->p_sys->b_clipped_out )
824 p_vout->p_sys->b_got_frame = true;
825 aglSwapBuffers(p_vout->p_sys->agl_ctx);
834 /* Enter this function with the p_vout locked */
835 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds )
837 // mozilla plugin provides coordinates based on port bounds
838 // however AGL coordinates are based on window structure region
839 // and are vertically flipped
841 CGrafPtr port = (CGrafPtr)p_vout->p_sys->agl_drawable;
842 Rect winBounds, clientBounds;
844 GetWindowBounds(GetWindowFromPort(port),
845 kWindowStructureRgn, &winBounds);
846 GetWindowBounds(GetWindowFromPort(port),
847 kWindowContentRgn, &clientBounds);
849 /* update video clipping bounds in drawable */
850 rect[0] = (clientBounds.left-winBounds.left)
851 + clipBounds.left; // from window left edge
852 rect[1] = (winBounds.bottom-winBounds.top)
853 - (clientBounds.top-winBounds.top)
854 - clipBounds.bottom; // from window bottom edge
855 rect[2] = clipBounds.right-clipBounds.left; // width
856 rect[3] = clipBounds.bottom-clipBounds.top; // height
858 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT, rect);
859 aglEnable(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT);
861 /* update video internal bounds in drawable */
862 p_vout->p_sys->i_width = viewBounds.right-viewBounds.left;
863 p_vout->p_sys->i_height = viewBounds.bottom-viewBounds.top;
864 p_vout->p_sys->i_offx = -clipBounds.left - viewBounds.left;
865 p_vout->p_sys->i_offy = clipBounds.bottom + viewBounds.top
866 - p_vout->p_sys->i_height;
868 aglUpdateContext(p_vout->p_sys->agl_ctx);
871 //default window event handler
872 static pascal OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData)
874 OSStatus result = noErr;
875 UInt32 class = GetEventClass (event);
876 UInt32 kind = GetEventKind (event);
877 vout_thread_t *p_vout = (vout_thread_t *)userData;
879 result = CallNextEventHandler(nextHandler, event);
880 if(class == kEventClassCommand)
882 HICommand theHICommand;
883 GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL, sizeof( HICommand ), NULL, &theHICommand );
885 switch ( theHICommand.commandID )
888 result = eventNotHandledErr;
891 else if(class == kEventClassWindow)
894 Rect rectPort = {0,0,0,0};
896 GetEventParameter(event, kEventParamDirectObject, typeWindowRef, NULL, sizeof(WindowRef), NULL, &window);
900 GetPortBounds(GetWindowPort(window), &rectPort);
905 case kEventWindowClosed:
906 case kEventWindowZoomed:
907 case kEventWindowBoundsChanged:
911 result = eventNotHandledErr;
914 else if(class == kEventClassMouse)
918 case kEventMouseDown:
922 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
925 case kEventMouseButtonPrimary:
929 var_Get( p_vout, "mouse-button-down", &val );
931 var_Set( p_vout, "mouse-button-down", val );
934 case kEventMouseButtonSecondary:
938 var_Get( p_vout, "mouse-button-down", &val );
940 var_Set( p_vout, "mouse-button-down", val );
943 case kEventMouseButtonTertiary:
947 var_Get( p_vout, "mouse-button-down", &val );
949 var_Set( p_vout, "mouse-button-down", val );
953 result = eventNotHandledErr;
962 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
965 case kEventMouseButtonPrimary:
967 UInt32 clickCount = 0;
968 GetEventParameter(event, kEventParamClickCount, typeUInt32, NULL, sizeof(clickCount), NULL, &clickCount);
974 var_Set((vout_thread_t *) p_vout->p_parent, "fullscreen", val);
981 var_GetCoords( p_vout, "mouse-moved", &x, &y );
982 var_SetCoords( p_vout, "mouse-clicked", x, y );
984 var_Get( p_vout, "mouse-button-down", &val );
986 var_Set( p_vout, "mouse-button-down", val );
990 case kEventMouseButtonSecondary:
994 var_Get( p_vout, "mouse-button-down", &val );
996 var_Set( p_vout, "mouse-button-down", val );
999 case kEventMouseButtonTertiary:
1003 var_Get( p_vout, "mouse-button-down", &val );
1005 var_Set( p_vout, "mouse-button-down", val );
1009 result = eventNotHandledErr;
1014 case kEventMouseMoved:
1019 unsigned int i_x, i_y;
1020 unsigned int i_height = p_vout->p_sys->i_height;
1021 unsigned int i_width = p_vout->p_sys->i_width;
1024 vout_PlacePicture(p_vout, i_width, i_height, &i_x, &i_y, &i_width, &i_height);
1026 GetEventParameter(event, kEventParamWindowMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &ml);
1028 x = (((int)ml.h) - i_x) * p_vout->render.i_width / i_width;
1029 y = (((int)ml.v) - i_y) * p_vout->render.i_height / i_height;
1030 var_SetCoords( p_vout, "mouse-moved", x, y );
1035 result = eventNotHandledErr;
1038 else if(class == kEventClassTextInput)
1042 case kEventTextInputUnicodeForKeyEvent:
1047 result = eventNotHandledErr;
1050 else if(class == kEventClassVLCPlugin)
1054 case kEventVLCPluginShowFullscreen:
1055 ShowWindow (p_vout->p_sys->theWindow);
1056 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
1057 //CGDisplayHideCursor(kCGDirectMainDisplay);
1059 case kEventVLCPluginHideFullscreen:
1060 HideWindow (p_vout->p_sys->theWindow);
1061 SetSystemUIMode( kUIModeNormal, 0);
1062 CGDisplayShowCursor(kCGDirectMainDisplay);
1065 result = eventNotHandledErr;
1072 static int aglLock( vout_thread_t * p_vout )
1074 /* get the underlying CGL context */
1075 CGLContextObj cglContext;
1076 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
1078 if( kCGLNoError == CGLLockContext( cglContext ) )
1080 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
1087 static void aglUnlock( vout_thread_t * p_vout )
1089 /* get the underlying CGL context */
1090 CGLContextObj cglContext;
1091 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
1093 CGLUnlockContext( cglContext );