1 /*****************************************************************************
2 * voutgl.m: MacOS X OpenGL provider
3 *****************************************************************************
4 * Copyright (C) 2001-2004, 2007 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;
61 NSAutoreleasePool * o_pool;
63 VLCVoutView * o_vout_view;
67 /* Mozilla plugin-related variables */
70 AGLDrawable agl_drawable;
72 int i_width, i_height;
74 WindowGroupRef winGroup;
76 Rect clipBounds, viewBounds;
79 /*****************************************************************************
81 *****************************************************************************/
83 static int Init ( vout_thread_t * p_vout );
84 static void End ( vout_thread_t * p_vout );
85 static int Manage ( vout_thread_t * p_vout );
86 static int Control( vout_thread_t *, int, va_list );
87 static void Swap ( vout_thread_t * p_vout );
88 static int Lock ( vout_thread_t * p_vout );
89 static void Unlock ( vout_thread_t * p_vout );
91 static int aglInit ( vout_thread_t * p_vout );
92 static void aglEnd ( vout_thread_t * p_vout );
93 static int aglManage ( vout_thread_t * p_vout );
94 static int aglControl( vout_thread_t *, int, va_list );
95 static void aglSwap ( vout_thread_t * p_vout );
96 static int aglLock ( vout_thread_t * p_vout );
97 static void aglUnlock ( vout_thread_t * p_vout );
99 int OpenVideoGL ( vlc_object_t * p_this )
101 vout_thread_t * p_vout = (vout_thread_t *) p_this;
102 vlc_value_t value_drawable;
104 if( !CGDisplayUsesOpenGLAcceleration( kCGDirectMainDisplay ) )
106 msg_Warn( p_vout, "no OpenGL hardware acceleration found. "
107 "Video display might be slow" );
109 msg_Dbg( p_vout, "display is Quartz Extreme accelerated" );
111 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
112 if( p_vout->p_sys == NULL )
115 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
117 var_Get( p_vout->p_libvlc, "drawable", &value_drawable );
118 if( value_drawable.i_int != 0 )
120 static const GLint ATTRIBUTES[] = {
133 AGLPixelFormat pixFormat;
135 p_vout->p_sys->b_embedded = true;
137 pixFormat = aglChoosePixelFormat(NULL, 0, ATTRIBUTES);
138 if( NULL == pixFormat )
140 msg_Err( p_vout, "no screen renderer available for required attributes." );
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." );
152 // tell opengl not to sync buffer swap with vertical retrace (too inefficient)
154 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL, ¶m);
155 aglEnable(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL);
158 p_vout->pf_init = aglInit;
159 p_vout->pf_end = aglEnd;
160 p_vout->pf_manage = aglManage;
161 p_vout->pf_control = aglControl;
162 p_vout->pf_swap = aglSwap;
163 p_vout->pf_lock = aglLock;
164 p_vout->pf_unlock = aglUnlock;
168 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
170 p_vout->p_sys->b_embedded = false;
172 [VLCGLView performSelectorOnMainThread:@selector(initVout:) withObject:[NSValue valueWithPointer:p_vout] waitUntilDone:YES];
176 /* Check to see if initVout: was successfull */
178 if( !p_vout->p_sys->o_vout_view )
183 p_vout->pf_init = Init;
184 p_vout->pf_end = End;
185 p_vout->pf_manage = Manage;
186 p_vout->pf_control= Control;
187 p_vout->pf_swap = Swap;
188 p_vout->pf_lock = Lock;
189 p_vout->pf_unlock = Unlock;
191 p_vout->p_sys->b_got_frame = false;
196 void CloseVideoGL ( vlc_object_t * p_this )
198 vout_thread_t * p_vout = (vout_thread_t *) p_this;
200 msg_Dbg( p_this, "Closing" );
202 if( p_vout->p_sys->b_embedded )
204 aglDestroyContext(p_vout->p_sys->agl_ctx);
206 else if(VLCIntf && vlc_object_alive (VLCIntf))
208 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
210 /* Close the window */
211 [p_vout->p_sys->o_vout_view performSelectorOnMainThread:@selector(closeVout) withObject:NULL waitUntilDone:YES];
216 free( p_vout->p_sys );
219 static int Init( vout_thread_t * p_vout )
221 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
225 static void End( vout_thread_t * p_vout )
227 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
230 static int Manage( vout_thread_t * p_vout )
232 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
234 [p_vout->p_sys->o_glview reshape];
235 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
237 if( p_vout->i_changes & VOUT_CROP_CHANGE )
239 [p_vout->p_sys->o_glview reshape];
240 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
243 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
245 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
247 p_vout->b_fullscreen = !p_vout->b_fullscreen;
249 if( p_vout->b_fullscreen )
250 [p_vout->p_sys->o_vout_view enterFullscreen];
252 [p_vout->p_sys->o_vout_view leaveFullscreen];
256 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
259 if( p_vout->p_sys->o_vout_view )
260 [p_vout->p_sys->o_vout_view manage];
264 /*****************************************************************************
265 * Control: control facility for the vout
266 *****************************************************************************/
267 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
273 case VOUT_SET_STAY_ON_TOP:
274 b_arg = (bool) va_arg( args, int );
275 [p_vout->p_sys->o_vout_view setOnTop: b_arg];
281 return vout_vaControlDefault( p_vout, i_query, args );
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
420 NSRect bounds = [self bounds];
422 var_Get( p_vout, "macosx-stretch", &val );
425 x = bounds.size.width;
426 y = bounds.size.height;
428 else if( bounds.size.height * p_vout->fmt_in.i_visible_width *
429 p_vout->fmt_in.i_sar_num <
430 bounds.size.width * p_vout->fmt_in.i_visible_height *
431 p_vout->fmt_in.i_sar_den )
433 x = ( bounds.size.height * p_vout->fmt_in.i_visible_width *
434 p_vout->fmt_in.i_sar_num ) /
435 ( p_vout->fmt_in.i_visible_height * p_vout->fmt_in.i_sar_den);
437 y = bounds.size.height;
441 x = bounds.size.width;
442 y = ( bounds.size.width * p_vout->fmt_in.i_visible_height *
443 p_vout->fmt_in.i_sar_den) /
444 ( p_vout->fmt_in.i_visible_width * p_vout->fmt_in.i_sar_num );
447 glViewport( ( bounds.size.width - x ) / 2,
448 ( bounds.size.height - y ) / 2, x, y );
452 if( p_vout->p_sys->b_got_frame )
454 /* Ask the opengl module to redraw */
455 vout_thread_t * p_parent;
456 p_parent = (vout_thread_t *) p_vout->p_parent;
458 if( p_parent && p_parent->pf_display )
460 p_parent->pf_display( p_parent, NULL );
465 glClear( GL_COLOR_BUFFER_BIT );
477 - (void) drawRect: (NSRect) rect
480 [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
481 [super drawRect:rect];
487 /*****************************************************************************
488 * embedded AGL context implementation
489 *****************************************************************************/
491 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds );
492 static void aglReshape( vout_thread_t * p_vout );
493 static OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
495 static int aglInit( vout_thread_t * p_vout )
502 var_Get( p_vout->p_libvlc, "drawable", &val );
503 p_vout->p_sys->agl_drawable = (AGLDrawable)val.i_int;
504 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
506 var_Get( p_vout->p_libvlc, "drawable-view-top", &val );
507 viewBounds.top = val.i_int;
508 var_Get( p_vout->p_libvlc, "drawable-view-left", &val );
509 viewBounds.left = val.i_int;
510 var_Get( p_vout->p_libvlc, "drawable-view-bottom", &val );
511 viewBounds.bottom = val.i_int;
512 var_Get( p_vout->p_libvlc, "drawable-view-right", &val );
513 viewBounds.right = val.i_int;
514 var_Get( p_vout->p_libvlc, "drawable-clip-top", &val );
515 clipBounds.top = val.i_int;
516 var_Get( p_vout->p_libvlc, "drawable-clip-left", &val );
517 clipBounds.left = val.i_int;
518 var_Get( p_vout->p_libvlc, "drawable-clip-bottom", &val );
519 clipBounds.bottom = val.i_int;
520 var_Get( p_vout->p_libvlc, "drawable-clip-right", &val );
521 clipBounds.right = val.i_int;
523 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
524 || (clipBounds.left == clipBounds.right);
525 if( ! p_vout->p_sys->b_clipped_out )
528 aglSetViewport(p_vout, viewBounds, clipBounds);
532 p_vout->p_sys->clipBounds = clipBounds;
533 p_vout->p_sys->viewBounds = viewBounds;
538 static void aglEnd( vout_thread_t * p_vout )
540 aglSetCurrentContext(NULL);
541 if( p_vout->p_sys->theWindow ) DisposeWindow( p_vout->p_sys->theWindow );
544 static void aglReshape( vout_thread_t * p_vout )
547 unsigned int i_height = p_vout->p_sys->i_height;
548 unsigned int i_width = p_vout->p_sys->i_width;
550 vout_PlacePicture(p_vout, i_width, i_height, &x, &y, &i_width, &i_height);
552 glViewport( p_vout->p_sys->i_offx + x, p_vout->p_sys->i_offy + y, i_width, i_height );
554 if( p_vout->p_sys->b_got_frame )
556 /* Ask the opengl module to redraw */
557 vout_thread_t * p_parent;
558 p_parent = (vout_thread_t *) p_vout->p_parent;
559 if( p_parent && p_parent->pf_display )
561 p_parent->pf_display( p_parent, NULL );
566 glClear( GL_COLOR_BUFFER_BIT );
570 /* private event class */
573 kEventClassVLCPlugin = 'vlcp',
575 /* private event kinds */
578 kEventVLCPluginShowFullscreen = 32768,
579 kEventVLCPluginHideFullscreen,
582 static void sendEventToMainThread(EventTargetRef target, UInt32 class, UInt32 kind)
585 if( noErr == CreateEvent(NULL, class, kind, 0, kEventAttributeNone, &myEvent) )
587 if( noErr == SetEventParameter(myEvent, kEventParamPostTarget, typeEventTargetRef, sizeof(EventTargetRef), &target) )
589 PostEventToQueue(GetMainEventQueue(), myEvent, kEventPriorityStandard);
591 ReleaseEvent(myEvent);
595 static int aglManage( vout_thread_t * p_vout )
597 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
602 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
604 if( p_vout->i_changes & VOUT_CROP_CHANGE )
609 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
611 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
613 aglSetDrawable(p_vout->p_sys->agl_ctx, NULL);
615 if( p_vout->b_fullscreen )
617 /* Close the fullscreen window and resume normal drawing */
622 var_Get( p_vout->p_libvlc, "drawable", &val );
623 p_vout->p_sys->agl_drawable = (AGLDrawable)val.i_int;
624 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
626 var_Get( p_vout->p_libvlc, "drawable-view-top", &val );
627 viewBounds.top = val.i_int;
628 var_Get( p_vout->p_libvlc, "drawable-view-left", &val );
629 viewBounds.left = val.i_int;
630 var_Get( p_vout->p_libvlc, "drawable-view-bottom", &val );
631 viewBounds.bottom = val.i_int;
632 var_Get( p_vout->p_libvlc, "drawable-view-right", &val );
633 viewBounds.right = val.i_int;
634 var_Get( p_vout->p_libvlc, "drawable-clip-top", &val );
635 clipBounds.top = val.i_int;
636 var_Get( p_vout->p_libvlc, "drawable-clip-left", &val );
637 clipBounds.left = val.i_int;
638 var_Get( p_vout->p_libvlc, "drawable-clip-bottom", &val );
639 clipBounds.bottom = val.i_int;
640 var_Get( p_vout->p_libvlc, "drawable-clip-right", &val );
641 clipBounds.right = val.i_int;
643 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
644 aglSetViewport(p_vout, viewBounds, clipBounds);
646 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty update to the main thread */
647 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow), kEventClassVLCPlugin, kEventVLCPluginHideFullscreen);
653 GDHandle deviceHdl = GetMainDevice();
654 deviceRect = (*deviceHdl)->gdRect;
656 if( !p_vout->p_sys->theWindow )
658 /* Create a window */
659 WindowAttributes windowAttrs;
661 windowAttrs = kWindowStandardDocumentAttributes
662 | kWindowStandardHandlerAttribute
663 | kWindowLiveResizeAttribute
664 | kWindowNoShadowAttribute;
666 windowAttrs &= (~kWindowResizableAttribute);
668 CreateNewWindow(kDocumentWindowClass, windowAttrs, &deviceRect, &p_vout->p_sys->theWindow);
669 if( !p_vout->p_sys->winGroup )
671 CreateWindowGroup(0, &p_vout->p_sys->winGroup);
672 SetWindowGroup(p_vout->p_sys->theWindow, p_vout->p_sys->winGroup);
673 SetWindowGroupParent( p_vout->p_sys->winGroup, GetWindowGroupOfClass(kDocumentWindowClass) ) ;
677 CFStringRef titleKey = CFSTR("Fullscreen VLC media plugin");
678 CFStringRef windowTitle = CFCopyLocalizedString(titleKey, NULL);
679 SetWindowTitleWithCFString(p_vout->p_sys->theWindow, windowTitle);
681 CFRelease(windowTitle);
683 //Install event handler
684 static const EventTypeSpec win_events[] = {
685 { kEventClassMouse, kEventMouseDown },
686 { kEventClassMouse, kEventMouseMoved },
687 { kEventClassMouse, kEventMouseUp },
688 { kEventClassWindow, kEventWindowClosed },
689 { kEventClassWindow, kEventWindowBoundsChanged },
690 { kEventClassCommand, kEventCommandProcess },
691 { kEventClassVLCPlugin, kEventVLCPluginShowFullscreen },
692 { kEventClassVLCPlugin, kEventVLCPluginHideFullscreen },
694 InstallWindowEventHandler (p_vout->p_sys->theWindow, NewEventHandlerUPP (WindowEventHandler), GetEventTypeCount(win_events), win_events, p_vout, NULL);
698 /* just in case device resolution changed */
699 SetWindowBounds(p_vout->p_sys->theWindow, kWindowContentRgn, &deviceRect);
701 glClear( GL_COLOR_BUFFER_BIT );
702 p_vout->p_sys->agl_drawable = (AGLDrawable)GetWindowPort(p_vout->p_sys->theWindow);
703 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
704 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
705 aglSetViewport(p_vout, deviceRect, deviceRect);
706 //aglSetFullScreen(p_vout->p_sys->agl_ctx, device_width, device_height, 0, 0);
708 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty update to the main thread */
709 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow), kEventClassVLCPlugin, kEventVLCPluginShowFullscreen);
713 p_vout->b_fullscreen = !p_vout->b_fullscreen;
714 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
719 static int aglControl( vout_thread_t *p_vout, int i_query, va_list args )
723 case VOUT_SET_VIEWPORT:
725 Rect viewBounds, clipBounds;
726 viewBounds.top = va_arg( args, int);
727 viewBounds.left = va_arg( args, int);
728 viewBounds.bottom = va_arg( args, int);
729 viewBounds.right = va_arg( args, int);
730 clipBounds.top = va_arg( args, int);
731 clipBounds.left = va_arg( args, int);
732 clipBounds.bottom = va_arg( args, int);
733 clipBounds.right = va_arg( args, int);
735 if( !p_vout->b_fullscreen )
738 ** check that the clip rect is not empty, as this is used
739 ** by Firefox to prevent a plugin from displaying during
740 ** a scrolling event. In this case we just prevent buffers
741 ** from being swapped and ignore clipping as this is less
742 ** disruptive than a GL geometry change
745 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
746 || (clipBounds.left == clipBounds.right);
747 if( ! p_vout->p_sys->b_clipped_out )
749 /* ignore consecutive viewport update with identical parameters */
750 if( memcmp(&clipBounds, &(p_vout->p_sys->clipBounds), sizeof(clipBounds) )
751 && memcmp(&viewBounds, &(p_vout->p_sys->viewBounds), sizeof(viewBounds)) )
754 aglSetViewport(p_vout, viewBounds, clipBounds);
755 aglReshape( p_vout );
757 p_vout->p_sys->clipBounds = clipBounds;
758 p_vout->p_sys->viewBounds = viewBounds;
765 case VOUT_REDRAW_RECT:
767 vout_thread_t * p_parent;
770 areaBounds.top = va_arg( args, int);
771 areaBounds.left = va_arg( args, int);
772 areaBounds.bottom = va_arg( args, int);
773 areaBounds.right = va_arg( args, int);
775 /* Ask the opengl module to redraw */
776 p_parent = (vout_thread_t *) p_vout->p_parent;
777 if( p_parent && p_parent->pf_display )
779 p_parent->pf_display( p_parent, NULL );
786 AGLDrawable drawable = (AGLDrawable)va_arg( args, int);
787 if( !p_vout->b_fullscreen && drawable != p_vout->p_sys->agl_drawable )
789 p_vout->p_sys->agl_drawable = drawable;
790 aglSetDrawable(p_vout->p_sys->agl_ctx, drawable);
796 return vout_vaControlDefault( p_vout, i_query, args );
800 static void aglSwap( vout_thread_t * p_vout )
802 if( ! p_vout->p_sys->b_clipped_out )
804 p_vout->p_sys->b_got_frame = true;
805 aglSwapBuffers(p_vout->p_sys->agl_ctx);
814 /* Enter this function with the p_vout locked */
815 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds )
817 // mozilla plugin provides coordinates based on port bounds
818 // however AGL coordinates are based on window structure region
819 // and are vertically flipped
821 CGrafPtr port = (CGrafPtr)p_vout->p_sys->agl_drawable;
822 Rect winBounds, clientBounds;
824 GetWindowBounds(GetWindowFromPort(port),
825 kWindowStructureRgn, &winBounds);
826 GetWindowBounds(GetWindowFromPort(port),
827 kWindowContentRgn, &clientBounds);
829 /* update video clipping bounds in drawable */
830 rect[0] = (clientBounds.left-winBounds.left)
831 + clipBounds.left; // from window left edge
832 rect[1] = (winBounds.bottom-winBounds.top)
833 - (clientBounds.top-winBounds.top)
834 - clipBounds.bottom; // from window bottom edge
835 rect[2] = clipBounds.right-clipBounds.left; // width
836 rect[3] = clipBounds.bottom-clipBounds.top; // height
837 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT, rect);
838 aglEnable(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT);
840 /* update video internal bounds in drawable */
841 p_vout->p_sys->i_width = viewBounds.right-viewBounds.left;
842 p_vout->p_sys->i_height = viewBounds.bottom-viewBounds.top;
843 p_vout->p_sys->i_offx = -clipBounds.left - viewBounds.left;
844 p_vout->p_sys->i_offy = clipBounds.bottom + viewBounds.top
845 - p_vout->p_sys->i_height;
847 aglUpdateContext(p_vout->p_sys->agl_ctx);
850 //default window event handler
851 static pascal OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData)
853 OSStatus result = noErr;
854 UInt32 class = GetEventClass (event);
855 UInt32 kind = GetEventKind (event);
856 vout_thread_t *p_vout = (vout_thread_t *)userData;
858 result = CallNextEventHandler(nextHandler, event);
859 if(class == kEventClassCommand)
861 HICommand theHICommand;
862 GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL, sizeof( HICommand ), NULL, &theHICommand );
864 switch ( theHICommand.commandID )
867 result = eventNotHandledErr;
870 else if(class == kEventClassWindow)
873 Rect rectPort = {0,0,0,0};
875 GetEventParameter(event, kEventParamDirectObject, typeWindowRef, NULL, sizeof(WindowRef), NULL, &window);
879 GetPortBounds(GetWindowPort(window), &rectPort);
884 case kEventWindowClosed:
885 case kEventWindowZoomed:
886 case kEventWindowBoundsChanged:
890 result = eventNotHandledErr;
893 else if(class == kEventClassMouse)
897 case kEventMouseDown:
901 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
904 case kEventMouseButtonPrimary:
908 var_Get( p_vout, "mouse-button-down", &val );
910 var_Set( p_vout, "mouse-button-down", val );
913 case kEventMouseButtonSecondary:
917 var_Get( p_vout, "mouse-button-down", &val );
919 var_Set( p_vout, "mouse-button-down", val );
922 case kEventMouseButtonTertiary:
926 var_Get( p_vout, "mouse-button-down", &val );
928 var_Set( p_vout, "mouse-button-down", val );
932 result = eventNotHandledErr;
941 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
944 case kEventMouseButtonPrimary:
946 UInt32 clickCount = 0;
947 GetEventParameter(event, kEventParamClickCount, typeUInt32, NULL, sizeof(clickCount), NULL, &clickCount);
953 var_Set((vout_thread_t *) p_vout->p_parent, "fullscreen", val);
960 var_Set( p_vout, "mouse-clicked", val );
962 var_Get( p_vout, "mouse-button-down", &val );
964 var_Set( p_vout, "mouse-button-down", val );
968 case kEventMouseButtonSecondary:
972 var_Get( p_vout, "mouse-button-down", &val );
974 var_Set( p_vout, "mouse-button-down", val );
977 case kEventMouseButtonTertiary:
981 var_Get( p_vout, "mouse-button-down", &val );
983 var_Set( p_vout, "mouse-button-down", val );
987 result = eventNotHandledErr;
992 case kEventMouseMoved:
997 unsigned int i_x, i_y;
998 unsigned int i_height = p_vout->p_sys->i_height;
999 unsigned int i_width = p_vout->p_sys->i_width;
1001 vout_PlacePicture(p_vout, i_width, i_height, &i_x, &i_y, &i_width, &i_height);
1003 GetEventParameter(event, kEventParamWindowMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &ml);
1005 val.i_int = ( ((int)ml.h) - i_x ) *
1006 p_vout->render.i_width / i_width;
1007 var_Set( p_vout, "mouse-x", val );
1009 val.i_int = ( ((int)ml.v) - i_y ) *
1010 p_vout->render.i_height / i_height;
1012 var_Set( p_vout, "mouse-y", val );
1015 var_Set( p_vout, "mouse-moved", val );
1021 result = eventNotHandledErr;
1024 else if(class == kEventClassTextInput)
1028 case kEventTextInputUnicodeForKeyEvent:
1033 result = eventNotHandledErr;
1036 else if(class == kEventClassVLCPlugin)
1040 case kEventVLCPluginShowFullscreen:
1041 ShowWindow (p_vout->p_sys->theWindow);
1042 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
1043 //CGDisplayHideCursor(kCGDirectMainDisplay);
1045 case kEventVLCPluginHideFullscreen:
1046 HideWindow (p_vout->p_sys->theWindow);
1047 SetSystemUIMode( kUIModeNormal, 0);
1048 CGDisplayShowCursor(kCGDirectMainDisplay);
1051 result = eventNotHandledErr;
1058 static int aglLock( vout_thread_t * p_vout )
1060 /* get the underlying CGL context */
1061 CGLContextObj cglContext;
1062 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
1064 if( kCGLNoError == CGLLockContext( cglContext ) )
1066 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
1073 static void aglUnlock( vout_thread_t * p_vout )
1075 /* get the underlying CGL context */
1076 CGLContextObj cglContext;
1077 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
1079 CGLUnlockContext( cglContext );