voutgl.m
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:36k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * voutgl.m: MacOS X OpenGL provider
  3.  *****************************************************************************
  4.  * Copyright (C) 2001-2004, 2007-2009 the VideoLAN team
  5.  * $Id: a6e751d8bd07c838a11aaeab5f3c294951657261 $
  6.  *
  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>
  14.  *
  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.
  19.  *
  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.
  24.  *
  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.  *****************************************************************************/
  29. /*****************************************************************************
  30.  * Preamble
  31.  *****************************************************************************/
  32. #include <errno.h>                                                 /* ENOMEM */
  33. #include <stdlib.h>                                                /* free() */
  34. #include <string.h>
  35. #include <vlc_keys.h>
  36. #include "intf.h"
  37. #include "vout.h"
  38. #include <OpenGL/OpenGL.h>
  39. #include <OpenGL/gl.h>
  40. #include <AGL/agl.h>
  41. /*****************************************************************************
  42.  * VLCGLView interface
  43.  *****************************************************************************/
  44. @interface VLCGLView : NSOpenGLView <VLCVoutViewResetting>
  45. {
  46.     vout_thread_t * p_vout;
  47. }
  48. + (void)resetVout: (NSValue *) voutValue;
  49. - (id) initWithVout: (vout_thread_t *) p_vout;
  50. @end
  51. struct vout_sys_t
  52. {
  53.     VLCGLView         * o_glview;
  54.     VLCVoutView       * o_vout_view;
  55.     bool                b_saved_frame;
  56.     NSRect              s_frame;
  57.     bool                b_got_frame;
  58.     /* Mozilla plugin-related variables (not 64bit compatible) */
  59.     bool                b_embedded;
  60. #ifndef __x86_64__
  61.     AGLContext          agl_ctx;
  62.     AGLDrawable         agl_drawable;
  63.     int                 i_offx, i_offy;
  64.     int                 i_width, i_height;
  65.     WindowRef           theWindow;
  66.     WindowGroupRef      winGroup;
  67.     bool                b_clipped_out;
  68.     Rect                clipBounds, viewBounds;
  69. #endif
  70. };
  71. /*****************************************************************************
  72.  * Local prototypes
  73.  *****************************************************************************/
  74. static int  Init   ( vout_thread_t * p_vout );
  75. static void End    ( vout_thread_t * p_vout );
  76. static int  Manage ( vout_thread_t * p_vout );
  77. static int  Control( vout_thread_t *, int, va_list );
  78. static void Swap   ( vout_thread_t * p_vout );
  79. static int  Lock   ( vout_thread_t * p_vout );
  80. static void Unlock ( vout_thread_t * p_vout );
  81. #ifndef __x86_64__
  82. static int  aglInit   ( vout_thread_t * p_vout );
  83. static void aglEnd    ( vout_thread_t * p_vout );
  84. static int  aglManage ( vout_thread_t * p_vout );
  85. static int  aglControl( vout_thread_t *, int, va_list );
  86. static void aglSwap   ( vout_thread_t * p_vout );
  87. static int  aglLock   ( vout_thread_t * p_vout );
  88. static void aglUnlock ( vout_thread_t * p_vout );
  89. #endif
  90. int OpenVideoGL  ( vlc_object_t * p_this )
  91. {
  92.     vout_thread_t * p_vout = (vout_thread_t *) p_this;
  93.     vlc_value_t value_drawable;
  94.     if( !CGDisplayUsesOpenGLAcceleration( kCGDirectMainDisplay ) )
  95.     {
  96.         msg_Warn( p_vout, "no OpenGL hardware acceleration found. "
  97.                           "Video display might be slow" );
  98.     }
  99.     msg_Dbg( p_vout, "display is Quartz Extreme accelerated" );
  100.     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
  101.     if( p_vout->p_sys == NULL )
  102.         return VLC_ENOMEM;
  103.     memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
  104. #ifndef __x86_64__
  105.     var_Get( p_vout->p_libvlc, "drawable-agl", &value_drawable );
  106.     if( value_drawable.i_int != 0 )
  107.     {
  108.         static const GLint ATTRIBUTES[] = {
  109.             AGL_WINDOW,
  110.             AGL_RGBA,
  111.             AGL_NO_RECOVERY,
  112.             AGL_ACCELERATED,
  113.             AGL_DOUBLEBUFFER,
  114.             AGL_RED_SIZE,   8,
  115.             AGL_GREEN_SIZE, 8,
  116.             AGL_BLUE_SIZE,  8,
  117.             AGL_ALPHA_SIZE, 8,
  118.             AGL_DEPTH_SIZE, 24,
  119.             AGL_NONE };
  120.         AGLPixelFormat pixFormat;
  121.         p_vout->p_sys->b_embedded = true;
  122.         pixFormat = aglChoosePixelFormat(NULL, 0, ATTRIBUTES);
  123.         if( NULL == pixFormat )
  124.         {
  125.             msg_Err( p_vout, "no screen renderer available for required attributes." );
  126.             free( p_vout->p_sys );
  127.             return VLC_EGENERIC;
  128.         }
  129.  
  130.         p_vout->p_sys->agl_ctx = aglCreateContext(pixFormat, NULL);
  131.         aglDestroyPixelFormat(pixFormat);
  132.         if( NULL == p_vout->p_sys->agl_ctx )
  133.         {
  134.             msg_Err( p_vout, "cannot create AGL context." );
  135.             free( p_vout->p_sys );
  136.             return VLC_EGENERIC;
  137.         }
  138.         else
  139.         {
  140.             // tell opengl not to sync buffer swap with vertical retrace (too inefficient)
  141.             GLint param = 0;
  142.             aglSetInteger(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL, &param);
  143.             aglEnable(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL);
  144.         }
  145.         p_vout->pf_init             = aglInit;
  146.         p_vout->pf_end              = aglEnd;
  147.         p_vout->pf_manage           = aglManage;
  148.         p_vout->pf_control          = aglControl;
  149.         p_vout->pf_swap             = aglSwap;
  150.         p_vout->pf_lock             = aglLock;
  151.         p_vout->pf_unlock           = aglUnlock;
  152.     }
  153.     else
  154.     {
  155. #endif
  156.         NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
  157.         p_vout->p_sys->b_embedded = false;
  158.         [VLCGLView performSelectorOnMainThread:@selector(initVout:) withObject:[NSValue valueWithPointer:p_vout] waitUntilDone:YES];
  159.         [o_pool release];
  160.         /* Check to see if initVout: was successfull */
  161.         if( !p_vout->p_sys->o_vout_view )
  162.         {
  163.             free( p_vout->p_sys );
  164.             return VLC_EGENERIC;
  165.         }
  166.         p_vout->pf_init   = Init;
  167.         p_vout->pf_end    = End;
  168.         p_vout->pf_manage = Manage;
  169.         p_vout->pf_control= Control;
  170.         p_vout->pf_swap   = Swap;
  171.         p_vout->pf_lock   = Lock;
  172.         p_vout->pf_unlock = Unlock;
  173. #ifndef __x86_64__
  174.     }
  175. #endif
  176.     p_vout->p_sys->b_got_frame = false;
  177.     return VLC_SUCCESS;
  178. }
  179. void CloseVideoGL ( vlc_object_t * p_this )
  180. {
  181.     vout_thread_t * p_vout = (vout_thread_t *) p_this;
  182.     msg_Dbg( p_this, "Closing" );
  183. #ifndef __x86_64__
  184.     if( p_vout->p_sys->b_embedded )
  185.     {
  186.         /* If the fullscreen window is still open, close it */
  187.         if( p_vout->b_fullscreen )
  188.         {
  189.             p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
  190.             aglManage( p_vout );
  191.             var_SetBool( p_vout->p_parent, "fullscreen", false );
  192.         }
  193.         if( p_vout->p_sys->agl_ctx )
  194.         {
  195.             aglEnd( p_vout );
  196.             aglDestroyContext(p_vout->p_sys->agl_ctx);
  197.         }
  198.     }
  199.     else
  200. #endif
  201.     if(VLCIntf && vlc_object_alive (VLCIntf))
  202.     {
  203.         NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
  204.         /* Close the window */
  205.         [p_vout->p_sys->o_vout_view performSelectorOnMainThread:@selector(closeVout) withObject:NULL waitUntilDone:YES];
  206.         [o_pool release];
  207.     }
  208.     /* Clean up */
  209.     free( p_vout->p_sys );
  210. }
  211. static int Init( vout_thread_t * p_vout )
  212. {
  213.     [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
  214.     return VLC_SUCCESS;
  215. }
  216. static void End( vout_thread_t * p_vout )
  217. {
  218.     [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
  219. }
  220. static int Manage( vout_thread_t * p_vout )
  221. {
  222.     if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
  223.     {
  224.         [p_vout->p_sys->o_glview reshape];
  225.         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
  226.     }
  227.     if( p_vout->i_changes & VOUT_CROP_CHANGE )
  228.     {
  229.         [p_vout->p_sys->o_glview reshape];
  230.         p_vout->i_changes &= ~VOUT_CROP_CHANGE;
  231.     }
  232.     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
  233.     {
  234.         NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
  235.         p_vout->b_fullscreen = !p_vout->b_fullscreen;
  236.         if( p_vout->b_fullscreen )
  237.             [p_vout->p_sys->o_vout_view enterFullscreen];
  238.         else
  239.             [p_vout->p_sys->o_vout_view leaveFullscreen];
  240.         [o_pool release];
  241.         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
  242.     }
  243.     if( p_vout->p_sys->o_vout_view )
  244.         [p_vout->p_sys->o_vout_view manage];
  245.     return VLC_SUCCESS;
  246. }
  247. /*****************************************************************************
  248.  * Control: control facility for the vout
  249.  *****************************************************************************/
  250. static int Control( vout_thread_t *p_vout, int i_query, va_list args )
  251. {
  252.     bool b_arg;
  253.     switch( i_query )
  254.     {
  255.         case VOUT_SET_STAY_ON_TOP:
  256.             b_arg = (bool) va_arg( args, int );
  257.             [p_vout->p_sys->o_vout_view setOnTop: b_arg];
  258.             return VLC_SUCCESS;
  259.         default:
  260.             return VLC_EGENERIC;
  261.     }
  262. }
  263. static void Swap( vout_thread_t * p_vout )
  264. {
  265.     p_vout->p_sys->b_got_frame = true;
  266.     [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
  267. }
  268. static int Lock( vout_thread_t * p_vout )
  269. {
  270.     if( kCGLNoError == CGLLockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]) )
  271.     {
  272.         [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
  273.         return 0;
  274.     }
  275.     return 1;
  276. }
  277. static void Unlock( vout_thread_t * p_vout )
  278. {
  279.     CGLUnlockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]);
  280. }
  281. /*****************************************************************************
  282.  * VLCGLView implementation
  283.  *****************************************************************************/
  284. @implementation VLCGLView
  285. + (void)initVout:(NSValue *)arg
  286. {
  287.     vout_thread_t * p_vout = [arg pointerValue];
  288.     /* Create the GL view */
  289.     p_vout->p_sys->o_glview = [[VLCGLView alloc] initWithVout: p_vout];
  290.     [p_vout->p_sys->o_glview autorelease];
  291.     /* Spawn the window */
  292.     id old_vout = p_vout->p_sys->o_vout_view;
  293.     p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
  294.                                                 subView: p_vout->p_sys->o_glview frame: nil] retain];
  295.     [old_vout release];
  296. }
  297. /* This function will reset the o_vout_view. It's useful to go fullscreen. */
  298. + (void)resetVout:(NSValue *) voutValue
  299. {
  300.     vout_thread_t * p_vout = [voutValue pointerValue];
  301.     if( p_vout->b_fullscreen )
  302.     {
  303.         /* Save window size and position */
  304.         p_vout->p_sys->s_frame.size =
  305.             [p_vout->p_sys->o_vout_view frame].size;
  306.         p_vout->p_sys->s_frame.origin =
  307.             [[p_vout->p_sys->o_vout_view voutWindow]frame].origin;
  308.         p_vout->p_sys->b_saved_frame = true;
  309.     }
  310.     [p_vout->p_sys->o_vout_view closeVout];
  311. #define o_glview p_vout->p_sys->o_glview
  312.     o_glview = [[VLCGLView alloc] initWithVout: p_vout];
  313.     [o_glview autorelease];
  314.  
  315.     if( p_vout->p_sys->b_saved_frame )
  316.     {
  317.         id old_vout = p_vout->p_sys->o_vout_view;
  318.         p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
  319.                                                     subView: o_glview
  320.                                                       frame: &p_vout->p_sys->s_frame] retain];
  321.         [old_vout release];
  322.     }
  323.     else
  324.     {
  325.         id old_vout = p_vout->p_sys->o_vout_view;
  326.         p_vout->p_sys->o_vout_view = [[VLCVoutView voutView: p_vout
  327.                                                     subView: o_glview frame: nil] retain];
  328.         [old_vout release];
  329.     }
  330. #undef o_glview
  331. }
  332. - (id) initWithVout: (vout_thread_t *) vout
  333. {
  334.     /* Must be called from main thread:
  335.      * "The NSView class is generally thread-safe, with a few exceptions. You
  336.      * should create, destroy, resize, move, and perform other operations on NSView
  337.      * objects only from the main thread of an application. Drawing from secondary
  338.      * threads is thread-safe as long as you bracket drawing calls with calls to
  339.      * lockFocusIfCanDraw and unlockFocus." Cocoa Thread Safety */
  340.     p_vout = vout;
  341.     NSOpenGLPixelFormatAttribute attribs[] =
  342.     {
  343.         NSOpenGLPFADoubleBuffer,
  344.         NSOpenGLPFAAccelerated,
  345.         NSOpenGLPFANoRecovery,
  346.         NSOpenGLPFAColorSize, 24,
  347.         NSOpenGLPFAAlphaSize, 8,
  348.         NSOpenGLPFADepthSize, 24,
  349.         NSOpenGLPFAWindow,
  350.         0
  351.     };
  352.     NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
  353.         initWithAttributes: attribs];
  354.     if( !fmt )
  355.     {
  356.         msg_Warn( p_vout, "could not create OpenGL video output" );
  357.         return nil;
  358.     }
  359.     self = [super initWithFrame: NSMakeRect(0,0,10,10) pixelFormat: fmt];
  360.     [fmt release];
  361.     [[self openGLContext] makeCurrentContext];
  362.     [[self openGLContext] update];
  363.     /* Swap buffers only during the vertical retrace of the monitor.
  364.        http://developer.apple.com/documentation/GraphicsImaging/
  365.        Conceptual/OpenGL/chap5/chapter_5_section_44.html */
  366.     GLint params[] = { 1 };
  367.     CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval, params );
  368.     return self;
  369. }
  370. - (BOOL)mouseDownCanMoveWindow
  371. {
  372.     return YES;
  373. }
  374. - (void) reshape
  375. {
  376.     int x, y;
  377.     Lock( p_vout );
  378.     NSRect bounds = [self bounds];
  379.     if( var_GetBool( p_vout, "macosx-stretch" ) )
  380.     {
  381.         x = bounds.size.width;
  382.         y = bounds.size.height;
  383.     }
  384.     else if( bounds.size.height * p_vout->fmt_in.i_visible_width *
  385.              p_vout->fmt_in.i_sar_num <
  386.              bounds.size.width * p_vout->fmt_in.i_visible_height *
  387.              p_vout->fmt_in.i_sar_den )
  388.     {
  389.         x = ( bounds.size.height * p_vout->fmt_in.i_visible_width *
  390.               p_vout->fmt_in.i_sar_num ) /
  391.             ( p_vout->fmt_in.i_visible_height * p_vout->fmt_in.i_sar_den);
  392.         y = bounds.size.height;
  393.     }
  394.     else
  395.     {
  396.         x = bounds.size.width;
  397.         y = ( bounds.size.width * p_vout->fmt_in.i_visible_height *
  398.               p_vout->fmt_in.i_sar_den) /
  399.             ( p_vout->fmt_in.i_visible_width * p_vout->fmt_in.i_sar_num  );
  400.     }
  401.     glViewport( ( bounds.size.width - x ) / 2,
  402.                 ( bounds.size.height - y ) / 2, x, y );
  403.     [super reshape];
  404.     if( p_vout->p_sys->b_got_frame )
  405.     {
  406.         /* Ask the opengl module to redraw */
  407.         vout_thread_t * p_parent;
  408.         p_parent = (vout_thread_t *) p_vout->p_parent;
  409.         Unlock( p_vout );
  410.         if( p_parent && p_parent->pf_display )
  411.         {
  412.             p_parent->pf_display( p_parent, NULL );
  413.         }
  414.     }
  415.     else
  416.     {
  417.         glClear( GL_COLOR_BUFFER_BIT );
  418.         Unlock( p_vout );
  419.     }
  420. }
  421. - (void) update
  422. {
  423.     Lock( p_vout );
  424.     [super update];
  425.     Unlock( p_vout );
  426. }
  427. - (void) drawRect: (NSRect) rect
  428. {
  429.     Lock( p_vout );
  430.     [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
  431.     [super drawRect:rect];
  432.     Unlock( p_vout );
  433. }
  434. @end
  435. /*****************************************************************************
  436.  * embedded AGL context implementation
  437.  *****************************************************************************/
  438. #ifndef __x86_64__
  439. static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds );
  440. static void aglReshape( vout_thread_t * p_vout );
  441. static OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
  442. static int aglInit( vout_thread_t * p_vout )
  443. {
  444.     Rect viewBounds;
  445.     Rect clipBounds;
  446.  
  447.     p_vout->p_sys->agl_drawable = (AGLDrawable)
  448.             var_GetInteger( p_vout->p_libvlc, "drawable-agl" );
  449.     aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
  450.     viewBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-view-top" );
  451.     viewBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-view-left" );
  452.     viewBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-view-bottom" );
  453.     viewBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-view-right" );
  454.     clipBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-clip-top" );
  455.     clipBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-clip-left" );
  456.     clipBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-clip-bottom" );
  457.     clipBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-clip-right" );
  458.     p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
  459.                                  || (clipBounds.left == clipBounds.right);
  460.     if( ! p_vout->p_sys->b_clipped_out )
  461.     {
  462.         aglLock(p_vout);
  463.         aglSetViewport(p_vout, viewBounds, clipBounds);
  464.         aglReshape(p_vout);
  465.         aglUnlock(p_vout);
  466.     }
  467.     p_vout->p_sys->clipBounds = clipBounds;
  468.     p_vout->p_sys->viewBounds = viewBounds;
  469.     return VLC_SUCCESS;
  470. }
  471. static void aglEnd( vout_thread_t * p_vout )
  472. {
  473.     aglSetCurrentContext(NULL);
  474.     if( p_vout->p_sys->theWindow )
  475.     {
  476.         DisposeWindow( p_vout->p_sys->theWindow );
  477.         p_vout->p_sys->theWindow = NULL;
  478.     }
  479. }
  480. static void aglReshape( vout_thread_t * p_vout )
  481. {
  482.     unsigned int x, y;
  483.     unsigned int i_height = p_vout->p_sys->i_height;
  484.     unsigned int i_width  = p_vout->p_sys->i_width;
  485.     vout_PlacePicture(p_vout, i_width, i_height, &x, &y, &i_width, &i_height);
  486.     glViewport( p_vout->p_sys->i_offx + x, p_vout->p_sys->i_offy + y, i_width, i_height );
  487.     if( p_vout->p_sys->b_got_frame )
  488.     {
  489.         /* Ask the opengl module to redraw */
  490.         vout_thread_t * p_parent;
  491.         p_parent = (vout_thread_t *) p_vout->p_parent;
  492.         if( p_parent && p_parent->pf_display )
  493.         {
  494.             p_parent->pf_display( p_parent, NULL );
  495.         }
  496.     }
  497.     else
  498.     {
  499.         glClear( GL_COLOR_BUFFER_BIT );
  500.     }
  501. }
  502. /* private event class */
  503. enum
  504. {
  505.     kEventClassVLCPlugin = 'vlcp',
  506. };
  507. /* private event kinds */
  508. enum
  509. {
  510.     kEventVLCPluginShowFullscreen = 32768,
  511.     kEventVLCPluginHideFullscreen,
  512. };
  513. static void sendEventToMainThread(EventTargetRef target, UInt32 class, UInt32 kind)
  514. {
  515.     EventRef myEvent;
  516.     if( noErr == CreateEvent(NULL, class, kind, 0, kEventAttributeNone, &myEvent) )
  517.     {
  518.         if( noErr == SetEventParameter(myEvent, kEventParamPostTarget, typeEventTargetRef, sizeof(EventTargetRef), &target) )
  519.         {
  520.             PostEventToQueue(GetMainEventQueue(), myEvent, kEventPriorityStandard);
  521.         }
  522.         ReleaseEvent(myEvent);
  523.     }
  524. }
  525. static int aglManage( vout_thread_t * p_vout )
  526. {
  527.     if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
  528.     {
  529.         aglLock( p_vout );
  530.         aglReshape(p_vout);
  531.         aglUnlock( p_vout );
  532.         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
  533.     }
  534.     if( p_vout->i_changes & VOUT_CROP_CHANGE )
  535.     {
  536.         aglLock( p_vout );
  537.         aglReshape(p_vout);
  538.         aglUnlock( p_vout );
  539.         p_vout->i_changes &= ~VOUT_CROP_CHANGE;
  540.     }
  541.     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
  542.     {
  543.         aglSetDrawable(p_vout->p_sys->agl_ctx, NULL);
  544.         aglLock( p_vout );
  545.         if( p_vout->b_fullscreen )
  546.         {
  547.             /* Close the fullscreen window and resume normal drawing */
  548.             Rect viewBounds;
  549.             Rect clipBounds;
  550.             p_vout->p_sys->agl_drawable = (AGLDrawable)
  551.                     var_GetInteger( p_vout->p_libvlc, "drawable-agl" );
  552.             aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
  553.             viewBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-view-top" );
  554.             viewBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-view-left" );
  555.             viewBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-view-bottom" );
  556.             viewBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-view-right" );
  557.             clipBounds.top = var_GetInteger( p_vout->p_libvlc, "drawable-clip-top" );
  558.             clipBounds.left = var_GetInteger( p_vout->p_libvlc, "drawable-clip-left" );
  559.             clipBounds.bottom = var_GetInteger( p_vout->p_libvlc, "drawable-clip-bottom" );
  560.             clipBounds.right = var_GetInteger( p_vout->p_libvlc, "drawable-clip-right" );
  561.             aglSetCurrentContext(p_vout->p_sys->agl_ctx);
  562.             aglSetViewport(p_vout, viewBounds, clipBounds);
  563.             if( p_vout->p_sys->theWindow )
  564.             {
  565.                 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty
  566.                  * update to the main thread */
  567.                 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow),
  568.                                       kEventClassVLCPlugin, kEventVLCPluginHideFullscreen);
  569.             }
  570.         }
  571.         else
  572.         {
  573.             /* Go into fullscreen */
  574.             Rect deviceRect;
  575.  
  576.             GDHandle deviceHdl = GetMainDevice();
  577.             deviceRect = (*deviceHdl)->gdRect;
  578.  
  579.             if( !p_vout->p_sys->theWindow )
  580.             {
  581.                 /* Create a window */
  582.                 WindowAttributes windowAttrs;
  583.                 windowAttrs = kWindowStandardDocumentAttributes
  584.                             | kWindowStandardHandlerAttribute
  585.                             | kWindowLiveResizeAttribute
  586.                             | kWindowNoShadowAttribute;
  587.  
  588.                 windowAttrs &= (~kWindowResizableAttribute);
  589.                 CreateNewWindow(kDocumentWindowClass, windowAttrs, &deviceRect, &p_vout->p_sys->theWindow);
  590.                 if( !p_vout->p_sys->winGroup )
  591.                 {
  592.                     CreateWindowGroup(0, &p_vout->p_sys->winGroup);
  593.                     SetWindowGroup(p_vout->p_sys->theWindow, p_vout->p_sys->winGroup);
  594.                     SetWindowGroupParent( p_vout->p_sys->winGroup, GetWindowGroupOfClass(kDocumentWindowClass) ) ;
  595.                 }
  596.  
  597.                 // Window title
  598.                 CFStringRef titleKey    = CFSTR("Fullscreen VLC media plugin");
  599.                 CFStringRef windowTitle = CFCopyLocalizedString(titleKey, NULL);
  600.                 SetWindowTitleWithCFString(p_vout->p_sys->theWindow, windowTitle);
  601.                 CFRelease(titleKey);
  602.                 CFRelease(windowTitle);
  603.  
  604.                 //Install event handler
  605.                 static const EventTypeSpec win_events[] = {
  606.                     { kEventClassMouse, kEventMouseDown },
  607.                     { kEventClassMouse, kEventMouseMoved },
  608.                     { kEventClassMouse, kEventMouseUp },
  609.                     { kEventClassWindow, kEventWindowClosed },
  610.                     { kEventClassWindow, kEventWindowBoundsChanged },
  611.                     { kEventClassCommand, kEventCommandProcess },
  612.                     { kEventClassVLCPlugin, kEventVLCPluginShowFullscreen },
  613.                     { kEventClassVLCPlugin, kEventVLCPluginHideFullscreen },
  614.                 };
  615.                 InstallWindowEventHandler (p_vout->p_sys->theWindow, NewEventHandlerUPP (WindowEventHandler), GetEventTypeCount(win_events), win_events, p_vout, NULL);
  616.             }
  617.             else
  618.             {
  619.                 /* just in case device resolution changed */
  620.                 SetWindowBounds(p_vout->p_sys->theWindow, kWindowContentRgn, &deviceRect);
  621.             }
  622.             glClear( GL_COLOR_BUFFER_BIT );
  623.             p_vout->p_sys->agl_drawable = (AGLDrawable)GetWindowPort(p_vout->p_sys->theWindow);
  624.             aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
  625.             aglSetCurrentContext(p_vout->p_sys->agl_ctx);
  626.             aglSetViewport(p_vout, deviceRect, deviceRect);
  627.             //aglSetFullScreen(p_vout->p_sys->agl_ctx, device_width, device_height, 0, 0);
  628.             if( p_vout->p_sys->theWindow )
  629.             {
  630.                 /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty
  631.                  * update to the main thread */
  632.                 sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow),
  633.                                       kEventClassVLCPlugin, kEventVLCPluginShowFullscreen);
  634.             }
  635.         }
  636.         aglReshape(p_vout);
  637.         aglUnlock( p_vout );
  638.         p_vout->b_fullscreen = !p_vout->b_fullscreen;
  639.         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
  640.     }
  641.     return VLC_SUCCESS;
  642. }
  643. static int aglControl( vout_thread_t *p_vout, int i_query, va_list args )
  644. {
  645.     switch( i_query )
  646.     {
  647.         case VOUT_SET_VIEWPORT:
  648.         {
  649.             Rect viewBounds, clipBounds;
  650.             viewBounds.top = va_arg( args, int);
  651.             viewBounds.left = va_arg( args, int);
  652.             viewBounds.bottom = va_arg( args, int);
  653.             viewBounds.right = va_arg( args, int);
  654.             clipBounds.top = va_arg( args, int);
  655.             clipBounds.left = va_arg( args, int);
  656.             clipBounds.bottom = va_arg( args, int);
  657.             clipBounds.right = va_arg( args, int);
  658.  
  659.             if( !p_vout->b_fullscreen )
  660.             {
  661.                 /*
  662.                 ** check that the clip rect is not empty, as this is used
  663.                 ** by Firefox to prevent a plugin from displaying during
  664.                 ** a scrolling event. In this case we just prevent buffers
  665.                 ** from being swapped and ignore clipping as this is less
  666.                 ** disruptive than a GL geometry change
  667.                 */
  668.                 p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
  669.                                              || (clipBounds.left == clipBounds.right);
  670.                 if( ! p_vout->p_sys->b_clipped_out )
  671.                 {
  672.                     /* ignore consecutive viewport update with identical parameters */
  673.                     if( memcmp(&clipBounds, &(p_vout->p_sys->clipBounds), sizeof(clipBounds) )
  674.                      && memcmp(&viewBounds, &(p_vout->p_sys->viewBounds), sizeof(viewBounds)) )
  675.                     {
  676.                         aglLock( p_vout );
  677.                         aglSetViewport(p_vout, viewBounds, clipBounds);
  678.                         aglReshape( p_vout );
  679.                         aglUnlock( p_vout );
  680.                         p_vout->p_sys->clipBounds = clipBounds;
  681.                         p_vout->p_sys->viewBounds = viewBounds;
  682.                     }
  683.                 }
  684.             }
  685.             return VLC_SUCCESS;
  686.         }
  687.         case VOUT_REDRAW_RECT:
  688.         {
  689.             vout_thread_t * p_parent;
  690.             Rect areaBounds;
  691.             areaBounds.top = va_arg( args, int);
  692.             areaBounds.left = va_arg( args, int);
  693.             areaBounds.bottom = va_arg( args, int);
  694.             areaBounds.right = va_arg( args, int);
  695.             /* Ask the opengl module to redraw */
  696.             p_parent = (vout_thread_t *) p_vout->p_parent;
  697.             if( p_parent && p_parent->pf_display )
  698.             {
  699.                 p_parent->pf_display( p_parent, NULL );
  700.             }
  701.             return VLC_SUCCESS;
  702.         }
  703.         default:
  704.             return VLC_EGENERIC;
  705.     }
  706. }
  707. static void aglSwap( vout_thread_t * p_vout )
  708. {
  709.     if( ! p_vout->p_sys->b_clipped_out )
  710.     {
  711.         p_vout->p_sys->b_got_frame = true;
  712.         aglSwapBuffers(p_vout->p_sys->agl_ctx);
  713.     }
  714.     else
  715.     {
  716.         /* drop frame */
  717.         glFlush();
  718.     }
  719. }
  720. /* Enter this function with the p_vout locked */
  721. static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds )
  722. {
  723.     // mozilla plugin provides coordinates based on port bounds
  724.     // however AGL coordinates are based on window structure region
  725.     // and are vertically flipped
  726.     GLint rect[4];
  727.     CGrafPtr port = (CGrafPtr)p_vout->p_sys->agl_drawable;
  728.     Rect winBounds, clientBounds;
  729.     GetWindowBounds(GetWindowFromPort(port),
  730.         kWindowStructureRgn, &winBounds);
  731.     GetWindowBounds(GetWindowFromPort(port),
  732.         kWindowContentRgn, &clientBounds);
  733.     /* update video clipping bounds in drawable */
  734.     rect[0] = (clientBounds.left-winBounds.left)
  735.             + clipBounds.left;                  // from window left edge
  736.     rect[1] = (winBounds.bottom-winBounds.top)
  737.             - (clientBounds.top-winBounds.top)
  738.             - clipBounds.bottom;                // from window bottom edge
  739.     rect[2] = clipBounds.right-clipBounds.left; // width
  740.     rect[3] = clipBounds.bottom-clipBounds.top; // height
  741.     aglSetInteger(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT, rect);
  742.     aglEnable(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT);
  743.     /* update video internal bounds in drawable */
  744.     p_vout->p_sys->i_width  = viewBounds.right-viewBounds.left;
  745.     p_vout->p_sys->i_height = viewBounds.bottom-viewBounds.top;
  746.     p_vout->p_sys->i_offx   = -clipBounds.left - viewBounds.left;
  747.     p_vout->p_sys->i_offy   = clipBounds.bottom + viewBounds.top
  748.                             - p_vout->p_sys->i_height;
  749.     aglUpdateContext(p_vout->p_sys->agl_ctx);
  750. }
  751. //default window event handler
  752. static pascal OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData)
  753. {
  754.     OSStatus result = noErr;
  755.     UInt32 class = GetEventClass (event);
  756.     UInt32 kind = GetEventKind (event);
  757.     vout_thread_t *p_vout = (vout_thread_t *)userData;
  758.     result = CallNextEventHandler(nextHandler, event);
  759.     if(class == kEventClassCommand)
  760.     {
  761.         HICommand theHICommand;
  762.         GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL, sizeof( HICommand ), NULL, &theHICommand );
  763.  
  764.         switch ( theHICommand.commandID )
  765.         {
  766.             default:
  767.                 result = eventNotHandledErr;
  768.         }
  769.     }
  770.     else if(class == kEventClassWindow)
  771.     {
  772.         WindowRef     window;
  773.         Rect          rectPort = {0,0,0,0};
  774.  
  775.         GetEventParameter(event, kEventParamDirectObject, typeWindowRef, NULL, sizeof(WindowRef), NULL, &window);
  776.         if(window)
  777.         {
  778.             GetPortBounds(GetWindowPort(window), &rectPort);
  779.         }
  780.         switch (kind)
  781.         {
  782.             case kEventWindowClosed:
  783.             case kEventWindowZoomed:
  784.             case kEventWindowBoundsChanged:
  785.                 break;
  786.  
  787.             default:
  788.                 result = eventNotHandledErr;
  789.         }
  790.     }
  791.     else if(class == kEventClassMouse)
  792.     {
  793.         switch (kind)
  794.         {
  795.             case kEventMouseDown:
  796.             {
  797.                 UInt16     button;
  798.  
  799.                 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
  800.                 switch (button)
  801.                 {
  802.                     case kEventMouseButtonPrimary:
  803.                     {
  804.                         vlc_value_t val;
  805.                         var_Get( p_vout, "mouse-button-down", &val );
  806.                         val.i_int |= 1;
  807.                         var_Set( p_vout, "mouse-button-down", val );
  808.                         break;
  809.                     }
  810.                     case kEventMouseButtonSecondary:
  811.                     {
  812.                         vlc_value_t val;
  813.                         var_Get( p_vout, "mouse-button-down", &val );
  814.                         val.i_int |= 2;
  815.                         var_Set( p_vout, "mouse-button-down", val );
  816.                         break;
  817.                     }
  818.                     case kEventMouseButtonTertiary:
  819.                     {
  820.                         vlc_value_t val;
  821.                         var_Get( p_vout, "mouse-button-down", &val );
  822.                         val.i_int |= 4;
  823.                         var_Set( p_vout, "mouse-button-down", val );
  824.                         break;
  825.                     }
  826.                     default:
  827.                         result = eventNotHandledErr;
  828.                 }
  829.                 break;
  830.             }
  831.             case kEventMouseUp:
  832.             {
  833.                 UInt16     button;
  834.  
  835.                 GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
  836.                 switch (button)
  837.                 {
  838.                     case kEventMouseButtonPrimary:
  839.                     {
  840.                         UInt32 clickCount = 0;
  841.                         GetEventParameter(event, kEventParamClickCount, typeUInt32, NULL, sizeof(clickCount), NULL, &clickCount);
  842.                         if( clickCount > 1 )
  843.                         {
  844.                             vlc_value_t val;
  845.                             val.b_bool = false;
  846.                             var_Set((vout_thread_t *) p_vout->p_parent, "fullscreen", val);
  847.                         }
  848.                         else
  849.                         {
  850.                             vlc_value_t val;
  851.                             var_SetBool( p_vout, "mouse-clicked", true );
  852.                             var_Get( p_vout, "mouse-button-down", &val );
  853.                             val.i_int &= ~1;
  854.                             var_Set( p_vout, "mouse-button-down", val );
  855.                         }
  856.                         break;
  857.                     }
  858.                     case kEventMouseButtonSecondary:
  859.                     {
  860.                         vlc_value_t val;
  861.                         var_Get( p_vout, "mouse-button-down", &val );
  862.                         val.i_int &= ~2;
  863.                         var_Set( p_vout, "mouse-button-down", val );
  864.                         break;
  865.                     }
  866.                     case kEventMouseButtonTertiary:
  867.                     {
  868.                         vlc_value_t val;
  869.                         var_Get( p_vout, "mouse-button-down", &val );
  870.                         val.i_int &= ~2;
  871.                         var_Set( p_vout, "mouse-button-down", val );
  872.                         break;
  873.                     }
  874.                     default:
  875.                         result = eventNotHandledErr;
  876.                 }
  877.                 break;
  878.             }
  879.             case kEventMouseMoved:
  880.             {
  881.                 Point ml;
  882.                 vlc_value_t val;
  883.                 unsigned int i_x, i_y;
  884.                 unsigned int i_height = p_vout->p_sys->i_height;
  885.                 unsigned int i_width  = p_vout->p_sys->i_width;
  886.                 vout_PlacePicture(p_vout, i_width, i_height, &i_x, &i_y, &i_width, &i_height);
  887.                 GetEventParameter(event, kEventParamWindowMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &ml);
  888.  
  889.                 val.i_int = ( ((int)ml.h) - i_x ) *
  890.                             p_vout->render.i_width / i_width;
  891.                 var_Set( p_vout, "mouse-x", val );
  892.                 val.i_int = ( ((int)ml.v) - i_y ) *
  893.                             p_vout->render.i_height / i_height;
  894.                 var_Set( p_vout, "mouse-y", val );
  895.                 val.b_bool = true;
  896.                 var_Set( p_vout, "mouse-moved", val );
  897.                 break;
  898.             }
  899.  
  900.             default:
  901.                 result = eventNotHandledErr;
  902.         }
  903.     }
  904.     else if(class == kEventClassTextInput)
  905.     {
  906.         switch (kind)
  907.         {
  908.             case kEventTextInputUnicodeForKeyEvent:
  909.             {
  910.                 break;
  911.             }
  912.             default:
  913.                 result = eventNotHandledErr;
  914.         }
  915.     }
  916.     else if(class == kEventClassVLCPlugin)
  917.     {
  918.         switch (kind)
  919.         {
  920.             case kEventVLCPluginShowFullscreen:
  921.                 ShowWindow (p_vout->p_sys->theWindow);
  922.                 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
  923.                 //CGDisplayHideCursor(kCGDirectMainDisplay);
  924.                 break;
  925.             case kEventVLCPluginHideFullscreen:
  926.                 HideWindow (p_vout->p_sys->theWindow);
  927.                 SetSystemUIMode( kUIModeNormal, 0);
  928.                 CGDisplayShowCursor(kCGDirectMainDisplay);
  929.                 break;
  930.             default:
  931.                 result = eventNotHandledErr;
  932.                 break;
  933.         }
  934.     }
  935.     return result;
  936. }
  937. static int aglLock( vout_thread_t * p_vout )
  938. {
  939. /* get the underlying CGL context */
  940.     CGLContextObj cglContext;
  941.     if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
  942.     {
  943.         if( kCGLNoError == CGLLockContext( cglContext ) )
  944.         {
  945.             aglSetCurrentContext(p_vout->p_sys->agl_ctx);
  946.             return 0;
  947.         }
  948.     }
  949.     return 1;
  950. }
  951. static void aglUnlock( vout_thread_t * p_vout )
  952. {
  953. /* get the underlying CGL context */
  954.     CGLContextObj cglContext;
  955.     if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
  956.     {
  957.         CGLUnlockContext( cglContext );
  958.     }
  959. }
  960. #endif