1 /*****************************************************************************
2 * voutgl.m: MacOS X OpenGL provider
3 *****************************************************************************
4 * Copyright (C) 2001-2004 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() */
35 #include <string.h> /* strerror() */
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: (vout_thread_t *)p_vout;
56 - (id) initWithVout: (vout_thread_t *) p_vout;
61 NSAutoreleasePool * o_pool;
63 VLCVoutView * o_vout_view;
64 vlc_bool_t b_saved_frame;
66 vlc_bool_t b_got_frame;
67 /* Mozilla plugin-related variables */
68 vlc_bool_t b_embedded;
70 AGLDrawable agl_drawable;
72 int i_width, i_height;
74 WindowGroupRef winGroup;
77 /*****************************************************************************
79 *****************************************************************************/
81 static int Init ( vout_thread_t * p_vout );
82 static void End ( vout_thread_t * p_vout );
83 static int Manage ( vout_thread_t * p_vout );
84 static int Control( vout_thread_t *, int, va_list );
85 static void Swap ( vout_thread_t * p_vout );
86 static int Lock ( vout_thread_t * p_vout );
87 static void Unlock ( vout_thread_t * p_vout );
89 static int aglInit ( vout_thread_t * p_vout );
90 static void aglEnd ( vout_thread_t * p_vout );
91 static int aglManage ( vout_thread_t * p_vout );
92 static int aglControl( vout_thread_t *, int, va_list );
93 static void aglSwap ( vout_thread_t * p_vout );
94 static int aglLock ( vout_thread_t * p_vout );
95 static void aglUnlock ( vout_thread_t * p_vout );
97 int E_(OpenVideoGL) ( vlc_object_t * p_this )
99 vout_thread_t * p_vout = (vout_thread_t *) p_this;
100 vlc_value_t value_drawable;
102 if( !CGDisplayUsesOpenGLAcceleration( kCGDirectMainDisplay ) )
104 msg_Warn( p_vout, "no OpenGL hardware acceleration found. "
105 "Video display will be slow" );
108 msg_Dbg( p_vout, "display is Quartz Extreme accelerated" );
110 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
111 if( p_vout->p_sys == NULL )
113 msg_Err( p_vout, "out of memory" );
117 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
119 var_Get( p_vout->p_libvlc, "drawable", &value_drawable );
120 if( value_drawable.i_int != 0 )
122 static const GLint ATTRIBUTES[] = {
135 AGLPixelFormat pixFormat;
137 p_vout->p_sys->b_embedded = VLC_TRUE;
139 pixFormat = aglChoosePixelFormat(NULL, 0, ATTRIBUTES);
140 if( NULL == pixFormat )
142 msg_Err( p_vout, "no screen renderer available for required attributes." );
146 p_vout->p_sys->agl_ctx = aglCreateContext(pixFormat, NULL);
147 aglDestroyPixelFormat(pixFormat);
148 if( NULL == p_vout->p_sys->agl_ctx )
150 msg_Err( p_vout, "cannot create AGL context." );
154 // tell opengl not to sync buffer swap with vertical retrace (too inefficient)
156 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL, ¶m);
157 aglEnable(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL);
160 p_vout->pf_init = aglInit;
161 p_vout->pf_end = aglEnd;
162 p_vout->pf_manage = aglManage;
163 p_vout->pf_control = aglControl;
164 p_vout->pf_swap = aglSwap;
165 p_vout->pf_lock = aglLock;
166 p_vout->pf_unlock = aglUnlock;
170 p_vout->p_sys->b_embedded = VLC_FALSE;
172 p_vout->p_sys->o_pool = [[NSAutoreleasePool alloc] init];
174 /* Create the GL view */
175 p_vout->p_sys->o_glview = [[VLCGLView alloc] initWithVout: p_vout];
176 [p_vout->p_sys->o_glview autorelease];
178 /* Spawn the window */
180 if( !(p_vout->p_sys->o_vout_view = [VLCVoutView getVoutView: p_vout
181 subView: p_vout->p_sys->o_glview frame: nil]) )
185 p_vout->pf_init = Init;
186 p_vout->pf_end = End;
187 p_vout->pf_manage = Manage;
188 p_vout->pf_control= Control;
189 p_vout->pf_swap = Swap;
190 p_vout->pf_lock = Lock;
191 p_vout->pf_unlock = Unlock;
193 p_vout->p_sys->b_got_frame = VLC_FALSE;
198 void E_(CloseVideoGL) ( vlc_object_t * p_this )
200 vout_thread_t * p_vout = (vout_thread_t *) p_this;
201 if( p_vout->p_sys->b_embedded )
203 aglDestroyContext(p_vout->p_sys->agl_ctx);
205 else if(!VLCIntf->b_die)
207 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
209 /* Close the window */
210 [p_vout->p_sys->o_vout_view closeVout];
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 [p_vout->p_sys->o_vout_view manage];
262 /*****************************************************************************
263 * Control: control facility for the vout
264 *****************************************************************************/
265 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
271 case VOUT_SET_STAY_ON_TOP:
272 b_arg = va_arg( args, vlc_bool_t );
273 [p_vout->p_sys->o_vout_view setOnTop: b_arg];
279 return vout_vaControlDefault( p_vout, i_query, args );
283 static void Swap( vout_thread_t * p_vout )
285 p_vout->p_sys->b_got_frame = VLC_TRUE;
286 [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
289 static int Lock( vout_thread_t * p_vout )
291 if( kCGLNoError == CGLLockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]) )
293 [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
299 static void Unlock( vout_thread_t * p_vout )
301 CGLUnlockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]);
304 /*****************************************************************************
305 * VLCGLView implementation
306 *****************************************************************************/
307 @implementation VLCGLView
309 /* This function will reset the o_vout_view. It's useful to go fullscreen. */
310 + (void)resetVout: (vout_thread_t *)p_vout
312 if( p_vout->b_fullscreen )
314 /* Save window size and position */
315 p_vout->p_sys->s_frame.size =
316 [p_vout->p_sys->o_vout_view frame].size;
317 p_vout->p_sys->s_frame.origin =
318 [[p_vout->p_sys->o_vout_view getWindow ]frame].origin;
319 p_vout->p_sys->b_saved_frame = VLC_TRUE;
322 [p_vout->p_sys->o_vout_view closeVout];
324 #define o_glview p_vout->p_sys->o_glview
325 o_glview = [[VLCGLView alloc] initWithVout: p_vout];
326 [o_glview autorelease];
328 if( p_vout->p_sys->b_saved_frame )
330 p_vout->p_sys->o_vout_view = [VLCVoutView getVoutView: p_vout
332 frame: &p_vout->p_sys->s_frame];
336 p_vout->p_sys->o_vout_view = [VLCVoutView getVoutView: p_vout
337 subView: o_glview frame: nil];
343 - (id) initWithVout: (vout_thread_t *) vout
347 NSOpenGLPixelFormatAttribute attribs[] =
349 NSOpenGLPFADoubleBuffer,
350 NSOpenGLPFAAccelerated,
351 NSOpenGLPFANoRecovery,
352 NSOpenGLPFAColorSize, 24,
353 NSOpenGLPFAAlphaSize, 8,
354 NSOpenGLPFADepthSize, 24,
359 NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
360 initWithAttributes: attribs];
364 msg_Warn( p_vout, "could not create OpenGL video output" );
368 self = [super initWithFrame: NSMakeRect(0,0,10,10) pixelFormat: fmt];
371 [[self openGLContext] makeCurrentContext];
372 [[self openGLContext] update];
374 /* Swap buffers only during the vertical retrace of the monitor.
375 http://developer.apple.com/documentation/GraphicsImaging/
376 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
377 long params[] = { 1 };
378 CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
389 NSRect bounds = [self bounds];
391 var_Get( p_vout, "macosx-stretch", &val );
394 x = bounds.size.width;
395 y = bounds.size.height;
397 else if( bounds.size.height * p_vout->fmt_in.i_visible_width *
398 p_vout->fmt_in.i_sar_num <
399 bounds.size.width * p_vout->fmt_in.i_visible_height *
400 p_vout->fmt_in.i_sar_den )
402 x = ( bounds.size.height * p_vout->fmt_in.i_visible_width *
403 p_vout->fmt_in.i_sar_num ) /
404 ( p_vout->fmt_in.i_visible_height * p_vout->fmt_in.i_sar_den);
406 y = bounds.size.height;
410 x = bounds.size.width;
411 y = ( bounds.size.width * p_vout->fmt_in.i_visible_height *
412 p_vout->fmt_in.i_sar_den) /
413 ( p_vout->fmt_in.i_visible_width * p_vout->fmt_in.i_sar_num );
416 glViewport( ( bounds.size.width - x ) / 2,
417 ( bounds.size.height - y ) / 2, x, y );
419 if( p_vout->p_sys->b_got_frame )
421 /* Ask the opengl module to redraw */
422 vout_thread_t * p_parent;
423 p_parent = (vout_thread_t *) p_vout->p_parent;
425 if( p_parent && p_parent->pf_display )
427 p_parent->pf_display( p_parent, NULL );
432 glClear( GL_COLOR_BUFFER_BIT );
445 - (void) drawRect: (NSRect) rect
448 [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
449 [super drawRect:rect];
455 /*****************************************************************************
456 * embedded AGL context implementation
457 *****************************************************************************/
459 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds );
460 static void aglReshape( vout_thread_t * p_vout );
461 static OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
463 static int aglInit( vout_thread_t * p_vout )
470 var_Get( p_vout->p_libvlc, "drawable", &val );
471 p_vout->p_sys->agl_drawable = (AGLDrawable)val.i_int;
472 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
474 var_Get( p_vout->p_libvlc, "drawable-view-top", &val );
475 viewBounds.top = val.i_int;
476 var_Get( p_vout->p_libvlc, "drawable-view-left", &val );
477 viewBounds.left = val.i_int;
478 var_Get( p_vout->p_libvlc, "drawable-view-bottom", &val );
479 viewBounds.bottom = val.i_int;
480 var_Get( p_vout->p_libvlc, "drawable-view-right", &val );
481 viewBounds.right = val.i_int;
482 var_Get( p_vout->p_libvlc, "drawable-clip-top", &val );
483 clipBounds.top = val.i_int;
484 var_Get( p_vout->p_libvlc, "drawable-clip-left", &val );
485 clipBounds.left = val.i_int;
486 var_Get( p_vout->p_libvlc, "drawable-clip-bottom", &val );
487 clipBounds.bottom = val.i_int;
488 var_Get( p_vout->p_libvlc, "drawable-clip-right", &val );
489 clipBounds.right = val.i_int;
492 aglSetViewport(p_vout, viewBounds, clipBounds);
499 static void aglEnd( vout_thread_t * p_vout )
501 aglSetCurrentContext(NULL);
502 if( p_vout->p_sys->theWindow ) DisposeWindow( p_vout->p_sys->theWindow );
505 static void aglReshape( vout_thread_t * p_vout )
508 unsigned int i_height = p_vout->p_sys->i_height;
509 unsigned int i_width = p_vout->p_sys->i_width;
511 vout_PlacePicture(p_vout, i_width, i_height, &x, &y, &i_width, &i_height);
513 glViewport( p_vout->p_sys->i_offx + x, p_vout->p_sys->i_offy + y, i_width, i_height );
515 if( p_vout->p_sys->b_got_frame )
517 /* Ask the opengl module to redraw */
518 vout_thread_t * p_parent;
519 p_parent = (vout_thread_t *) p_vout->p_parent;
520 if( p_parent && p_parent->pf_display )
522 p_parent->pf_display( p_parent, NULL );
527 glClear( GL_COLOR_BUFFER_BIT );
531 /* private event class */
534 kEventClassVLCPlugin = 'vlcp',
536 /* private event kinds */
539 kEventVLCPluginShowFullscreen = 32768,
540 kEventVLCPluginHideFullscreen,
543 static void sendEventToMainThread(EventTargetRef target, UInt32 class, UInt32 kind)
546 if( noErr == CreateEvent(NULL, class, kind, 0, kEventAttributeNone, &myEvent) )
548 if( noErr == SetEventParameter(myEvent, kEventParamPostTarget, typeEventTargetRef, sizeof(EventTargetRef), &target) )
550 PostEventToQueue(GetMainEventQueue(), myEvent, kEventPriorityStandard);
552 ReleaseEvent(myEvent);
556 static int aglManage( vout_thread_t * p_vout )
558 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
563 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
565 if( p_vout->i_changes & VOUT_CROP_CHANGE )
570 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
572 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
574 aglSetDrawable(p_vout->p_sys->agl_ctx, NULL);
576 if( p_vout->b_fullscreen )
578 /* Close the fullscreen window and resume normal drawing */
583 var_Get( p_vout->p_libvlc, "drawable", &val );
584 p_vout->p_sys->agl_drawable = (AGLDrawable)val.i_int;
585 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
587 var_Get( p_vout->p_libvlc, "drawable-view-top", &val );
588 viewBounds.top = val.i_int;
589 var_Get( p_vout->p_libvlc, "drawable-view-left", &val );
590 viewBounds.left = val.i_int;
591 var_Get( p_vout->p_libvlc, "drawable-view-bottom", &val );
592 viewBounds.bottom = val.i_int;
593 var_Get( p_vout->p_libvlc, "drawable-view-right", &val );
594 viewBounds.right = val.i_int;
595 var_Get( p_vout->p_libvlc, "drawable-clip-top", &val );
596 clipBounds.top = val.i_int;
597 var_Get( p_vout->p_libvlc, "drawable-clip-left", &val );
598 clipBounds.left = val.i_int;
599 var_Get( p_vout->p_libvlc, "drawable-clip-bottom", &val );
600 clipBounds.bottom = val.i_int;
601 var_Get( p_vout->p_libvlc, "drawable-clip-right", &val );
602 clipBounds.right = val.i_int;
604 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
605 aglSetViewport(p_vout, viewBounds, clipBounds);
607 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty update to the main thread */
608 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow), kEventClassVLCPlugin, kEventVLCPluginHideFullscreen);
614 GDHandle deviceHdl = GetMainDevice();
615 deviceRect = (*deviceHdl)->gdRect;
617 if( !p_vout->p_sys->theWindow )
619 /* Create a window */
620 WindowAttributes windowAttrs;
622 windowAttrs = kWindowStandardDocumentAttributes
623 | kWindowStandardHandlerAttribute
624 | kWindowLiveResizeAttribute
625 | kWindowNoShadowAttribute;
627 windowAttrs &= (~kWindowResizableAttribute);
629 CreateNewWindow(kDocumentWindowClass, windowAttrs, &deviceRect, &p_vout->p_sys->theWindow);
630 if( !p_vout->p_sys->winGroup )
632 CreateWindowGroup(0, &p_vout->p_sys->winGroup);
633 SetWindowGroup(p_vout->p_sys->theWindow, p_vout->p_sys->winGroup);
634 SetWindowGroupParent( p_vout->p_sys->winGroup, GetWindowGroupOfClass(kDocumentWindowClass) ) ;
638 CFStringRef titleKey = CFSTR("Fullscreen VLC media plugin");
639 CFStringRef windowTitle = CFCopyLocalizedString(titleKey, NULL);
640 SetWindowTitleWithCFString(p_vout->p_sys->theWindow, windowTitle);
642 CFRelease(windowTitle);
644 //Install event handler
645 static const EventTypeSpec win_events[] = {
646 { kEventClassMouse, kEventMouseDown },
647 { kEventClassMouse, kEventMouseMoved },
648 { kEventClassMouse, kEventMouseUp },
649 { kEventClassWindow, kEventWindowClosed },
650 { kEventClassWindow, kEventWindowBoundsChanged },
651 { kEventClassCommand, kEventCommandProcess },
652 { kEventClassVLCPlugin, kEventVLCPluginShowFullscreen },
653 { kEventClassVLCPlugin, kEventVLCPluginHideFullscreen },
655 InstallWindowEventHandler (p_vout->p_sys->theWindow, NewEventHandlerUPP (WindowEventHandler), GetEventTypeCount(win_events), win_events, p_vout, NULL);
659 /* just in case device resolution changed */
660 SetWindowBounds(p_vout->p_sys->theWindow, kWindowContentRgn, &deviceRect);
662 glClear( GL_COLOR_BUFFER_BIT );
663 p_vout->p_sys->agl_drawable = (AGLDrawable)GetWindowPort(p_vout->p_sys->theWindow);
664 aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
665 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
666 aglSetViewport(p_vout, deviceRect, deviceRect);
667 //aglSetFullScreen(p_vout->p_sys->agl_ctx, device_width, device_height, 0, 0);
669 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty update to the main thread */
670 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow), kEventClassVLCPlugin, kEventVLCPluginShowFullscreen);
674 p_vout->b_fullscreen = !p_vout->b_fullscreen;
675 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
680 static int aglControl( vout_thread_t *p_vout, int i_query, va_list args )
684 case VOUT_SET_VIEWPORT:
686 Rect viewBounds, clipBounds;
687 viewBounds.top = va_arg( args, int);
688 viewBounds.left = va_arg( args, int);
689 viewBounds.bottom = va_arg( args, int);
690 viewBounds.right = va_arg( args, int);
691 clipBounds.top = va_arg( args, int);
692 clipBounds.left = va_arg( args, int);
693 clipBounds.bottom = va_arg( args, int);
694 clipBounds.right = va_arg( args, int);
696 if( !p_vout->b_fullscreen )
699 aglSetViewport(p_vout, viewBounds, clipBounds);
700 aglReshape( p_vout );
708 AGLDrawable drawable = (AGLDrawable)va_arg( args, int);
709 if( !p_vout->b_fullscreen && drawable != p_vout->p_sys->agl_drawable )
711 p_vout->p_sys->agl_drawable = drawable;
712 aglSetDrawable(p_vout->p_sys->agl_ctx, drawable);
718 return vout_vaControlDefault( p_vout, i_query, args );
722 static void aglSwap( vout_thread_t * p_vout )
724 p_vout->p_sys->b_got_frame = VLC_TRUE;
725 aglSwapBuffers(p_vout->p_sys->agl_ctx);
728 /* Enter this function with the p_vout locked */
729 static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds )
731 // mozilla plugin provides coordinates based on port bounds
732 // however AGL coordinates are based on window structure region
733 // and are vertically flipped
735 CGrafPtr port = (CGrafPtr)p_vout->p_sys->agl_drawable;
736 Rect winBounds, clientBounds;
738 GetWindowBounds(GetWindowFromPort(port),
739 kWindowStructureRgn, &winBounds);
740 GetWindowBounds(GetWindowFromPort(port),
741 kWindowContentRgn, &clientBounds);
743 /* update video clipping bounds in drawable */
744 rect[0] = (clientBounds.left-winBounds.left)
745 + clipBounds.left; // from window left edge
746 rect[1] = (winBounds.bottom-winBounds.top)
747 - (clientBounds.top-winBounds.top)
748 - clipBounds.bottom; // from window bottom edge
749 rect[2] = clipBounds.right-clipBounds.left; // width
750 rect[3] = clipBounds.bottom-clipBounds.top; // height
751 aglSetInteger(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT, rect);
752 aglEnable(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT);
754 /* update video internal bounds in drawable */
755 p_vout->p_sys->i_width = viewBounds.right-viewBounds.left;
756 p_vout->p_sys->i_height = viewBounds.bottom-viewBounds.top;
757 p_vout->p_sys->i_offx = -clipBounds.left - viewBounds.left;
758 p_vout->p_sys->i_offy = clipBounds.bottom + viewBounds.top
759 - p_vout->p_sys->i_height;
761 aglUpdateContext(p_vout->p_sys->agl_ctx);
764 //default window event handler
765 static pascal OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData)
767 OSStatus result = noErr;
768 UInt32 class = GetEventClass (event);
769 UInt32 kind = GetEventKind (event);
770 vout_thread_t *p_vout = (vout_thread_t *)userData;
772 result = CallNextEventHandler(nextHandler, event);
773 if(class == kEventClassCommand)
775 HICommand theHICommand;
776 GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL, sizeof( HICommand ), NULL, &theHICommand );
778 switch ( theHICommand.commandID )
781 result = eventNotHandledErr;
784 else if(class == kEventClassWindow)
787 Rect rectPort = {0,0,0,0};
789 GetEventParameter(event, kEventParamDirectObject, typeWindowRef, NULL, sizeof(WindowRef), NULL, &window);
793 GetPortBounds(GetWindowPort(window), &rectPort);
798 case kEventWindowClosed:
799 case kEventWindowZoomed:
800 case kEventWindowBoundsChanged:
804 result = eventNotHandledErr;
807 else if(class == kEventClassMouse)
811 case kEventMouseDown:
815 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
818 case kEventMouseButtonPrimary:
822 var_Get( p_vout, "mouse-button-down", &val );
824 var_Set( p_vout, "mouse-button-down", val );
827 case kEventMouseButtonSecondary:
831 var_Get( p_vout, "mouse-button-down", &val );
833 var_Set( p_vout, "mouse-button-down", val );
836 case kEventMouseButtonTertiary:
840 var_Get( p_vout, "mouse-button-down", &val );
842 var_Set( p_vout, "mouse-button-down", val );
846 result = eventNotHandledErr;
855 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
858 case kEventMouseButtonPrimary:
860 UInt32 clickCount = 0;
861 GetEventParameter(event, kEventParamClickCount, typeUInt32, NULL, sizeof(clickCount), NULL, &clickCount);
866 val.b_bool = VLC_FALSE;
867 var_Set((vout_thread_t *) p_vout->p_parent, "fullscreen", val);
873 val.b_bool = VLC_TRUE;
874 var_Set( p_vout, "mouse-clicked", val );
876 var_Get( p_vout, "mouse-button-down", &val );
878 var_Set( p_vout, "mouse-button-down", val );
882 case kEventMouseButtonSecondary:
886 var_Get( p_vout, "mouse-button-down", &val );
888 var_Set( p_vout, "mouse-button-down", val );
891 case kEventMouseButtonTertiary:
895 var_Get( p_vout, "mouse-button-down", &val );
897 var_Set( p_vout, "mouse-button-down", val );
901 result = eventNotHandledErr;
906 case kEventMouseMoved:
911 unsigned int i_x, i_y;
912 unsigned int i_height = p_vout->p_sys->i_height;
913 unsigned int i_width = p_vout->p_sys->i_width;
915 vout_PlacePicture(p_vout, i_width, i_height, &i_x, &i_y, &i_width, &i_height);
917 GetEventParameter(event, kEventParamWindowMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &ml);
919 val.i_int = ( ((int)ml.h) - i_x ) *
920 p_vout->render.i_width / i_width;
921 var_Set( p_vout, "mouse-x", val );
923 val.i_int = ( ((int)ml.v) - i_y ) *
924 p_vout->render.i_height / i_height;
926 var_Set( p_vout, "mouse-y", val );
928 val.b_bool = VLC_TRUE;
929 var_Set( p_vout, "mouse-moved", val );
935 result = eventNotHandledErr;
938 else if(class == kEventClassTextInput)
942 case kEventTextInputUnicodeForKeyEvent:
947 result = eventNotHandledErr;
950 else if(class == kEventClassVLCPlugin)
954 case kEventVLCPluginShowFullscreen:
955 ShowWindow (p_vout->p_sys->theWindow);
956 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
957 //CGDisplayHideCursor(kCGDirectMainDisplay);
959 case kEventVLCPluginHideFullscreen:
960 HideWindow (p_vout->p_sys->theWindow);
961 SetSystemUIMode( kUIModeNormal, 0);
962 CGDisplayShowCursor(kCGDirectMainDisplay);
965 result = eventNotHandledErr;
972 static int aglLock( vout_thread_t * p_vout )
976 * before 10.4, we set the AGL context as current and
977 * then we retrieve and use the matching CGL context
979 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
980 return kCGLNoError != CGLLockContext( CGLGetCurrentContext() );
982 /* since 10.4, this is the safe way to get the underlying CGL context */
983 CGLContextObj cglContext;
984 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
986 if( kCGLNoError == CGLLockContext( cglContext ) )
988 aglSetCurrentContext(p_vout->p_sys->agl_ctx);
996 static void aglUnlock( vout_thread_t * p_vout )
1000 * before 10.4, we assume that the AGL context is current.
1001 * therefore, we use the current CGL context
1003 CGLUnlockContext( CGLGetCurrentContext() );
1005 /* since 10.4, this is the safe way to get the underlying CGL context */
1006 CGLContextObj cglContext;
1007 if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
1009 CGLUnlockContext( cglContext );