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;
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 */
177 if( !p_vout->p_sys->o_vout_view )
182 p_vout->pf_init = Init;
183 p_vout->pf_end = End;
184 p_vout->pf_manage = Manage;
185 p_vout->pf_control= Control;
186 p_vout->pf_swap = Swap;
187 p_vout->pf_lock = Lock;
188 p_vout->pf_unlock = Unlock;
190 p_vout->p_sys->b_got_frame = false;
195 void CloseVideoGL ( vlc_object_t * p_this )
197 vout_thread_t * p_vout = (vout_thread_t *) p_this;
199 msg_Dbg( p_this, "Closing" );
201 if( p_vout->p_sys->b_embedded )
203 aglDestroyContext(p_vout->p_sys->agl_ctx);
205 else if(VLCIntf && vlc_object_alive (VLCIntf))
207 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
209 /* Close the window */
210 [p_vout->p_sys->o_vout_view performSelectorOnMainThread:@selector(closeVout) withObject:NULL waitUntilDone:YES];
215 free( p_vout->p_sys );
218 static int Init( vout_thread_t * p_vout )
220 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
224 static void End( vout_thread_t * p_vout )
226 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
229 static int Manage( vout_thread_t * p_vout )
231 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
233 [p_vout->p_sys->o_glview reshape];
234 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
236 if( p_vout->i_changes & VOUT_CROP_CHANGE )
238 [p_vout->p_sys->o_glview reshape];
239 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
242 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
244 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
246 p_vout->b_fullscreen = !p_vout->b_fullscreen;
248 if( p_vout->b_fullscreen )
249 [p_vout->p_sys->o_vout_view enterFullscreen];
251 [p_vout->p_sys->o_vout_view leaveFullscreen];
255 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
258 if( p_vout->p_sys->o_vout_view )
259 [p_vout->p_sys->o_vout_view manage];
263 /*****************************************************************************
264 * Control: control facility for the vout
265 *****************************************************************************/
266 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
272 case VOUT_SET_STAY_ON_TOP:
273 b_arg = (bool) va_arg( args, int );
274 [p_vout->p_sys->o_vout_view setOnTop: b_arg];
282 static void Swap( vout_thread_t * p_vout )
284 p_vout->p_sys->b_got_frame = true;
285 [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
288 static int Lock( vout_thread_t * p_vout )
290 if( kCGLNoError == CGLLockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]) )
292 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
298 static void Unlock( vout_thread_t * p_vout )
300 CGLUnlockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]);
303 /*****************************************************************************
304 * VLCGLView implementation
305 *****************************************************************************/
306 @implementation VLCGLView
307 + (void)initVout:(NSValue *)arg
309 vout_thread_t * p_vout = [arg pointerValue];
311 /* Create the GL view */
312 p_vout->p_sys->o_glview = [[VLCGLView alloc] initWithVout: p_vout];
313 [p_vout->p_sys->o_glview autorelease];
315 /* Spawn the window */
316 id old_vout = p_vout->p_sys->o_vout_view;
317 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
318 subView: p_vout->p_sys->o_glview frame: nil] retain];
322 /* This function will reset the o_vout_view. It's useful to go fullscreen. */
323 + (void)resetVout:(NSValue *) voutValue
325 vout_thread_t * p_vout = [voutValue pointerValue];
326 if( p_vout->b_fullscreen )
328 /* Save window size and position */
329 p_vout->p_sys->s_frame.size =
330 [p_vout->p_sys->o_vout_view frame].size;
331 p_vout->p_sys->s_frame.origin =
332 [[p_vout->p_sys->o_vout_view getWindow ]frame].origin;
333 p_vout->p_sys->b_saved_frame = true;
336 [p_vout->p_sys->o_vout_view closeVout];
338 #define o_glview p_vout->p_sys->o_glview
339 o_glview = [[VLCGLView alloc] initWithVout: p_vout];
340 [o_glview autorelease];
342 if( p_vout->p_sys->b_saved_frame )
344 id old_vout = p_vout->p_sys->o_vout_view;
345 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
347 frame: &p_vout->p_sys->s_frame] retain];
352 id old_vout = p_vout->p_sys->o_vout_view;
353 p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
354 subView: o_glview frame: nil] retain];
360 - (id) initWithVout: (vout_thread_t *) vout
362 /* Must be called from main thread:
363 * "The NSView class is generally thread-safe, with a few exceptions. You
364 * should create, destroy, resize, move, and perform other operations on NSView
365 * objects only from the main thread of an application. Drawing from secondary
366 * threads is thread-safe as long as you bracket drawing calls with calls to
367 * lockFocusIfCanDraw and unlockFocus." Cocoa Thread Safety */
371 NSOpenGLPixelFormatAttribute attribs[] =
373 NSOpenGLPFADoubleBuffer,
374 NSOpenGLPFAAccelerated,
375 NSOpenGLPFANoRecovery,
376 NSOpenGLPFAColorSize, 24,
377 NSOpenGLPFAAlphaSize, 8,
378 NSOpenGLPFADepthSize, 24,
383 NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
384 initWithAttributes: attribs];
388 msg_Warn( p_vout, "could not create OpenGL video output" );
392 self = [super initWithFrame: NSMakeRect(0,0,10,10) pixelFormat: fmt];
395 [[self openGLContext] makeCurrentContext];
396 [[self openGLContext] update];
398 /* Swap buffers only during the vertical retrace of the monitor.
399 http://developer.apple.com/documentation/GraphicsImaging/
400 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
401 GLint params[] = { 1 };
402 CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval, params );
406 - (BOOL)mouseDownCanMoveWindow
417 NSRect bounds = [self bounds];
419 var_Get( p_vout, "macosx-stretch", &val );
422 x = bounds.size.width;
423 y = bounds.size.height;
425 else if( bounds.size.height * p_vout->fmt_in.i_visible_width *
426 p_vout->fmt_in.i_sar_num <
427 bounds.size.width * p_vout->fmt_in.i_visible_height *
428 p_vout->fmt_in.i_sar_den )
430 x = ( bounds.size.height * p_vout->fmt_in.i_visible_width *
431 p_vout->fmt_in.i_sar_num ) /
432 ( p_vout->fmt_in.i_visible_height * p_vout->fmt_in.i_sar_den);
434 y = bounds.size.height;
438 x = bounds.size.width;
439 y = ( bounds.size.width * p_vout->fmt_in.i_visible_height *
440 p_vout->fmt_in.i_sar_den) /
441 ( p_vout->fmt_in.i_visible_width * p_vout->fmt_in.i_sar_num );
444 glViewport( ( bounds.size.width - x ) / 2,
445 ( bounds.size.height - y ) / 2, x, y );
449 if( p_vout->p_sys->b_got_frame )
451 /* Ask the opengl module to redraw */
452 vout_thread_t * p_parent;
453 p_parent = (vout_thread_t *) p_vout->p_parent;
455 if( p_parent && p_parent->pf_display )
457 p_parent->pf_display( p_parent, NULL );
462 glClear( GL_COLOR_BUFFER_BIT );
474 - (void) drawRect: (NSRect) rect
477 [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
478 [super drawRect:rect];
484 /*****************************************************************************
485 * embedded AGL context implementation
486 *****************************************************************************/
488 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds );
489 static void aglReshape( vout_thread_t * p_vout );
490 static OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
492 static int aglInit( vout_thread_t * p_vout )
499 var_Get( p_vout->p_libvlc, "drawable", &val );
500 p_vout->p_sys->agl_drawable = (AGLDrawable)val.i_int;
501 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
503 var_Get( p_vout->p_libvlc, "drawable-view-top", &val );
504 viewBounds.top = val.i_int;
505 var_Get( p_vout->p_libvlc, "drawable-view-left", &val );
506 viewBounds.left = val.i_int;
507 var_Get( p_vout->p_libvlc, "drawable-view-bottom", &val );
508 viewBounds.bottom = val.i_int;
509 var_Get( p_vout->p_libvlc, "drawable-view-right", &val );
510 viewBounds.right = val.i_int;
511 var_Get( p_vout->p_libvlc, "drawable-clip-top", &val );
512 clipBounds.top = val.i_int;
513 var_Get( p_vout->p_libvlc, "drawable-clip-left", &val );
514 clipBounds.left = val.i_int;
515 var_Get( p_vout->p_libvlc, "drawable-clip-bottom", &val );
516 clipBounds.bottom = val.i_int;
517 var_Get( p_vout->p_libvlc, "drawable-clip-right", &val );
518 clipBounds.right = val.i_int;
520 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
521 || (clipBounds.left == clipBounds.right);
522 if( ! p_vout->p_sys->b_clipped_out )
525 aglSetViewport(p_vout, viewBounds, clipBounds);
529 p_vout->p_sys->clipBounds = clipBounds;
530 p_vout->p_sys->viewBounds = viewBounds;
535 static void aglEnd( vout_thread_t * p_vout )
537 aglSetCurrentContext(NULL);
538 if( p_vout->p_sys->theWindow ) DisposeWindow( p_vout->p_sys->theWindow );
541 static void aglReshape( vout_thread_t * p_vout )
544 unsigned int i_height = p_vout->p_sys->i_height;
545 unsigned int i_width = p_vout->p_sys->i_width;
547 vout_PlacePicture(p_vout, i_width, i_height, &x, &y, &i_width, &i_height);
549 glViewport( p_vout->p_sys->i_offx + x, p_vout->p_sys->i_offy + y, i_width, i_height );
551 if( p_vout->p_sys->b_got_frame )
553 /* Ask the opengl module to redraw */
554 vout_thread_t * p_parent;
555 p_parent = (vout_thread_t *) p_vout->p_parent;
556 if( p_parent && p_parent->pf_display )
558 p_parent->pf_display( p_parent, NULL );
563 glClear( GL_COLOR_BUFFER_BIT );
567 /* private event class */
570 kEventClassVLCPlugin = 'vlcp',
572 /* private event kinds */
575 kEventVLCPluginShowFullscreen = 32768,
576 kEventVLCPluginHideFullscreen,
579 static void sendEventToMainThread(EventTargetRef target, UInt32 class, UInt32 kind)
582 if( noErr == CreateEvent(NULL, class, kind, 0, kEventAttributeNone, &myEvent) )
584 if( noErr == SetEventParameter(myEvent, kEventParamPostTarget, typeEventTargetRef, sizeof(EventTargetRef), &target) )
586 PostEventToQueue(GetMainEventQueue(), myEvent, kEventPriorityStandard);
588 ReleaseEvent(myEvent);
592 static int aglManage( vout_thread_t * p_vout )
594 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
599 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
601 if( p_vout->i_changes & VOUT_CROP_CHANGE )
606 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
608 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
610 aglSetDrawable(p_vout->p_sys->agl_ctx, NULL);
612 if( p_vout->b_fullscreen )
614 /* Close the fullscreen window and resume normal drawing */
619 var_Get( p_vout->p_libvlc, "drawable", &val );
620 p_vout->p_sys->agl_drawable = (AGLDrawable)val.i_int;
621 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
623 var_Get( p_vout->p_libvlc, "drawable-view-top", &val );
624 viewBounds.top = val.i_int;
625 var_Get( p_vout->p_libvlc, "drawable-view-left", &val );
626 viewBounds.left = val.i_int;
627 var_Get( p_vout->p_libvlc, "drawable-view-bottom", &val );
628 viewBounds.bottom = val.i_int;
629 var_Get( p_vout->p_libvlc, "drawable-view-right", &val );
630 viewBounds.right = val.i_int;
631 var_Get( p_vout->p_libvlc, "drawable-clip-top", &val );
632 clipBounds.top = val.i_int;
633 var_Get( p_vout->p_libvlc, "drawable-clip-left", &val );
634 clipBounds.left = val.i_int;
635 var_Get( p_vout->p_libvlc, "drawable-clip-bottom", &val );
636 clipBounds.bottom = val.i_int;
637 var_Get( p_vout->p_libvlc, "drawable-clip-right", &val );
638 clipBounds.right = val.i_int;
640 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
641 aglSetViewport(p_vout, viewBounds, clipBounds);
643 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty update to the main thread */
644 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow), kEventClassVLCPlugin, kEventVLCPluginHideFullscreen);
650 GDHandle deviceHdl = GetMainDevice();
651 deviceRect = (*deviceHdl)->gdRect;
653 if( !p_vout->p_sys->theWindow )
655 /* Create a window */
656 WindowAttributes windowAttrs;
658 windowAttrs = kWindowStandardDocumentAttributes
659 | kWindowStandardHandlerAttribute
660 | kWindowLiveResizeAttribute
661 | kWindowNoShadowAttribute;
663 windowAttrs &= (~kWindowResizableAttribute);
665 CreateNewWindow(kDocumentWindowClass, windowAttrs, &deviceRect, &p_vout->p_sys->theWindow);
666 if( !p_vout->p_sys->winGroup )
668 CreateWindowGroup(0, &p_vout->p_sys->winGroup);
669 SetWindowGroup(p_vout->p_sys->theWindow, p_vout->p_sys->winGroup);
670 SetWindowGroupParent( p_vout->p_sys->winGroup, GetWindowGroupOfClass(kDocumentWindowClass) ) ;
674 CFStringRef titleKey = CFSTR("Fullscreen VLC media plugin");
675 CFStringRef windowTitle = CFCopyLocalizedString(titleKey, NULL);
676 SetWindowTitleWithCFString(p_vout->p_sys->theWindow, windowTitle);
678 CFRelease(windowTitle);
680 //Install event handler
681 static const EventTypeSpec win_events[] = {
682 { kEventClassMouse, kEventMouseDown },
683 { kEventClassMouse, kEventMouseMoved },
684 { kEventClassMouse, kEventMouseUp },
685 { kEventClassWindow, kEventWindowClosed },
686 { kEventClassWindow, kEventWindowBoundsChanged },
687 { kEventClassCommand, kEventCommandProcess },
688 { kEventClassVLCPlugin, kEventVLCPluginShowFullscreen },
689 { kEventClassVLCPlugin, kEventVLCPluginHideFullscreen },
691 InstallWindowEventHandler (p_vout->p_sys->theWindow, NewEventHandlerUPP (WindowEventHandler), GetEventTypeCount(win_events), win_events, p_vout, NULL);
695 /* just in case device resolution changed */
696 SetWindowBounds(p_vout->p_sys->theWindow, kWindowContentRgn, &deviceRect);
698 glClear( GL_COLOR_BUFFER_BIT );
699 p_vout->p_sys->agl_drawable = (AGLDrawable)GetWindowPort(p_vout->p_sys->theWindow);
700 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
701 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
702 aglSetViewport(p_vout, deviceRect, deviceRect);
703 //aglSetFullScreen(p_vout->p_sys->agl_ctx, device_width, device_height, 0, 0);
705 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty update to the main thread */
706 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow), kEventClassVLCPlugin, kEventVLCPluginShowFullscreen);
710 p_vout->b_fullscreen = !p_vout->b_fullscreen;
711 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
716 static int aglControl( vout_thread_t *p_vout, int i_query, va_list args )
720 case VOUT_SET_VIEWPORT:
722 Rect viewBounds, clipBounds;
723 viewBounds.top = va_arg( args, int);
724 viewBounds.left = va_arg( args, int);
725 viewBounds.bottom = va_arg( args, int);
726 viewBounds.right = va_arg( args, int);
727 clipBounds.top = va_arg( args, int);
728 clipBounds.left = va_arg( args, int);
729 clipBounds.bottom = va_arg( args, int);
730 clipBounds.right = va_arg( args, int);
732 if( !p_vout->b_fullscreen )
735 ** check that the clip rect is not empty, as this is used
736 ** by Firefox to prevent a plugin from displaying during
737 ** a scrolling event. In this case we just prevent buffers
738 ** from being swapped and ignore clipping as this is less
739 ** disruptive than a GL geometry change
742 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
743 || (clipBounds.left == clipBounds.right);
744 if( ! p_vout->p_sys->b_clipped_out )
746 /* ignore consecutive viewport update with identical parameters */
747 if( memcmp(&clipBounds, &(p_vout->p_sys->clipBounds), sizeof(clipBounds) )
748 && memcmp(&viewBounds, &(p_vout->p_sys->viewBounds), sizeof(viewBounds)) )
751 aglSetViewport(p_vout, viewBounds, clipBounds);
752 aglReshape( p_vout );
754 p_vout->p_sys->clipBounds = clipBounds;
755 p_vout->p_sys->viewBounds = viewBounds;
762 case VOUT_REDRAW_RECT:
764 vout_thread_t * p_parent;
767 areaBounds.top = va_arg( args, int);
768 areaBounds.left = va_arg( args, int);
769 areaBounds.bottom = va_arg( args, int);
770 areaBounds.right = va_arg( args, int);
772 /* Ask the opengl module to redraw */
773 p_parent = (vout_thread_t *) p_vout->p_parent;
774 if( p_parent && p_parent->pf_display )
776 p_parent->pf_display( p_parent, NULL );
786 static void aglSwap( vout_thread_t * p_vout )
788 if( ! p_vout->p_sys->b_clipped_out )
790 p_vout->p_sys->b_got_frame = true;
791 aglSwapBuffers(p_vout->p_sys->agl_ctx);
800 /* Enter this function with the p_vout locked */
801 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds )
803 // mozilla plugin provides coordinates based on port bounds
804 // however AGL coordinates are based on window structure region
805 // and are vertically flipped
807 CGrafPtr port = (CGrafPtr)p_vout->p_sys->agl_drawable;
808 Rect winBounds, clientBounds;
810 GetWindowBounds(GetWindowFromPort(port),
811 kWindowStructureRgn, &winBounds);
812 GetWindowBounds(GetWindowFromPort(port),
813 kWindowContentRgn, &clientBounds);
815 /* update video clipping bounds in drawable */
816 rect[0] = (clientBounds.left-winBounds.left)
817 + clipBounds.left; // from window left edge
818 rect[1] = (winBounds.bottom-winBounds.top)
819 - (clientBounds.top-winBounds.top)
820 - clipBounds.bottom; // from window bottom edge
821 rect[2] = clipBounds.right-clipBounds.left; // width
822 rect[3] = clipBounds.bottom-clipBounds.top; // height
823 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT, rect);
824 aglEnable(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT);
826 /* update video internal bounds in drawable */
827 p_vout->p_sys->i_width = viewBounds.right-viewBounds.left;
828 p_vout->p_sys->i_height = viewBounds.bottom-viewBounds.top;
829 p_vout->p_sys->i_offx = -clipBounds.left - viewBounds.left;
830 p_vout->p_sys->i_offy = clipBounds.bottom + viewBounds.top
831 - p_vout->p_sys->i_height;
833 aglUpdateContext(p_vout->p_sys->agl_ctx);
836 //default window event handler
837 static pascal OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData)
839 OSStatus result = noErr;
840 UInt32 class = GetEventClass (event);
841 UInt32 kind = GetEventKind (event);
842 vout_thread_t *p_vout = (vout_thread_t *)userData;
844 result = CallNextEventHandler(nextHandler, event);
845 if(class == kEventClassCommand)
847 HICommand theHICommand;
848 GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL, sizeof( HICommand ), NULL, &theHICommand );
850 switch ( theHICommand.commandID )
853 result = eventNotHandledErr;
856 else if(class == kEventClassWindow)
859 Rect rectPort = {0,0,0,0};
861 GetEventParameter(event, kEventParamDirectObject, typeWindowRef, NULL, sizeof(WindowRef), NULL, &window);
865 GetPortBounds(GetWindowPort(window), &rectPort);
870 case kEventWindowClosed:
871 case kEventWindowZoomed:
872 case kEventWindowBoundsChanged:
876 result = eventNotHandledErr;
879 else if(class == kEventClassMouse)
883 case kEventMouseDown:
887 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
890 case kEventMouseButtonPrimary:
894 var_Get( p_vout, "mouse-button-down", &val );
896 var_Set( p_vout, "mouse-button-down", val );
899 case kEventMouseButtonSecondary:
903 var_Get( p_vout, "mouse-button-down", &val );
905 var_Set( p_vout, "mouse-button-down", val );
908 case kEventMouseButtonTertiary:
912 var_Get( p_vout, "mouse-button-down", &val );
914 var_Set( p_vout, "mouse-button-down", val );
918 result = eventNotHandledErr;
927 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
930 case kEventMouseButtonPrimary:
932 UInt32 clickCount = 0;
933 GetEventParameter(event, kEventParamClickCount, typeUInt32, NULL, sizeof(clickCount), NULL, &clickCount);
939 var_Set((vout_thread_t *) p_vout->p_parent, "fullscreen", val);
945 var_SetBool( p_vout, "mouse-clicked", true );
947 var_Get( p_vout, "mouse-button-down", &val );
949 var_Set( p_vout, "mouse-button-down", val );
953 case kEventMouseButtonSecondary:
957 var_Get( p_vout, "mouse-button-down", &val );
959 var_Set( p_vout, "mouse-button-down", val );
962 case kEventMouseButtonTertiary:
966 var_Get( p_vout, "mouse-button-down", &val );
968 var_Set( p_vout, "mouse-button-down", val );
972 result = eventNotHandledErr;
977 case kEventMouseMoved:
982 unsigned int i_x, i_y;
983 unsigned int i_height = p_vout->p_sys->i_height;
984 unsigned int i_width = p_vout->p_sys->i_width;
986 vout_PlacePicture(p_vout, i_width, i_height, &i_x, &i_y, &i_width, &i_height);
988 GetEventParameter(event, kEventParamWindowMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &ml);
990 val.i_int = ( ((int)ml.h) - i_x ) *
991 p_vout->render.i_width / i_width;
992 var_Set( p_vout, "mouse-x", val );
994 val.i_int = ( ((int)ml.v) - i_y ) *
995 p_vout->render.i_height / i_height;
997 var_Set( p_vout, "mouse-y", val );
1000 var_Set( p_vout, "mouse-moved", val );
1006 result = eventNotHandledErr;
1009 else if(class == kEventClassTextInput)
1013 case kEventTextInputUnicodeForKeyEvent:
1018 result = eventNotHandledErr;
1021 else if(class == kEventClassVLCPlugin)
1025 case kEventVLCPluginShowFullscreen:
1026 ShowWindow (p_vout->p_sys->theWindow);
1027 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
1028 //CGDisplayHideCursor(kCGDirectMainDisplay);
1030 case kEventVLCPluginHideFullscreen:
1031 HideWindow (p_vout->p_sys->theWindow);
1032 SetSystemUIMode( kUIModeNormal, 0);
1033 CGDisplayShowCursor(kCGDirectMainDisplay);
1036 result = eventNotHandledErr;
1043 static int aglLock( vout_thread_t * p_vout )
1045 /* get the underlying CGL context */
1046 CGLContextObj cglContext;
1047 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
1049 if( kCGLNoError == CGLLockContext( cglContext ) )
1051 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
1058 static void aglUnlock( 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 CGLUnlockContext( cglContext );