voutgl.m 36.3 KB
Newer Older
Eric Petit's avatar
Eric Petit committed
1
/*****************************************************************************
2
 * voutgl.m: MacOS X OpenGL provider
Eric Petit's avatar
Eric Petit committed
3
 *****************************************************************************
4
 * Copyright (C) 2001-2004 the VideoLAN team
Felix Paul Kühne's avatar
Felix Paul Kühne committed
5
 * $Id$
Eric Petit's avatar
Eric Petit committed
6 7 8 9 10 11
 *
 * Authors: Colin Delacroix <colin@zoy.org>
 *          Florian G. Pflug <fgp@phlo.org>
 *          Jon Lech Johansen <jon-vl@nanocrew.net>
 *          Derk-Jan Hartman <hartman at videolan dot org>
 *          Eric Petit <titer@m0k.org>
12
 *          Benjamin Pracht <bigben at videolan dot org>
13
 *          Damien Fouilleul <damienf at videolan dot org>
Eric Petit's avatar
Eric Petit committed
14 15 16 17 18
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
19
 *
Eric Petit's avatar
Eric Petit committed
20 21 22 23 24 25 26
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
27
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Eric Petit's avatar
Eric Petit committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                                /* free() */
#include <string.h>                                            /* strerror() */

#include <vlc_keys.h>

#include "intf.h"
#include "vout.h"

#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>

45 46
#include <AGL/agl.h>

Eric Petit's avatar
Eric Petit committed
47
/*****************************************************************************
48
 * VLCGLView interface
Eric Petit's avatar
Eric Petit committed
49
 *****************************************************************************/
50
@interface VLCGLView : NSOpenGLView <VLCVoutViewResetting>
Eric Petit's avatar
Eric Petit committed
51 52 53 54
{
    vout_thread_t * p_vout;
}

55
+ (void)resetVout: (vout_thread_t *)p_vout;
56
- (id) initWithVout: (vout_thread_t *) p_vout;
Eric Petit's avatar
Eric Petit committed
57 58 59 60
@end

struct vout_sys_t
{
61 62
    NSAutoreleasePool * o_pool;
    VLCGLView         * o_glview;
63
    VLCVoutView       * o_vout_view;
64 65
    vlc_bool_t          b_saved_frame;
    NSRect              s_frame;
66
    vlc_bool_t          b_got_frame;
67 68 69 70 71 72
    /* Mozilla plugin-related variables */
    vlc_bool_t          b_embedded;
    AGLContext          agl_ctx;
    AGLDrawable         agl_drawable;
    int                 i_offx, i_offy;
    int                 i_width, i_height;
73 74
    WindowRef           theWindow;
    WindowGroupRef      winGroup;
75 76
    vlc_bool_t          b_clipped_out;
    Rect                clipBounds, viewBounds;             
Eric Petit's avatar
Eric Petit committed
77 78 79 80 81 82 83 84 85
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/

static int  Init   ( vout_thread_t * p_vout );
static void End    ( vout_thread_t * p_vout );
static int  Manage ( vout_thread_t * p_vout );
86
static int  Control( vout_thread_t *, int, va_list );
Eric Petit's avatar
Eric Petit committed
87
static void Swap   ( vout_thread_t * p_vout );
88 89
static int  Lock   ( vout_thread_t * p_vout );
static void Unlock ( vout_thread_t * p_vout );
Eric Petit's avatar
Eric Petit committed
90

91 92 93
static int  aglInit   ( vout_thread_t * p_vout );
static void aglEnd    ( vout_thread_t * p_vout );
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 97
static int  aglLock   ( vout_thread_t * p_vout );
static void aglUnlock ( vout_thread_t * p_vout );
98

Eric Petit's avatar
Eric Petit committed
99 100 101
int E_(OpenVideoGL)  ( vlc_object_t * p_this )
{
    vout_thread_t * p_vout = (vout_thread_t *) p_this;
102
    vlc_value_t value_drawable;
103

104 105
    if( !CGDisplayUsesOpenGLAcceleration( kCGDirectMainDisplay ) )
    {
106 107
        msg_Warn( p_vout, "no OpenGL hardware acceleration found. "
                          "Video display will be slow" );
108 109 110 111
        return( 1 );
    }
    msg_Dbg( p_vout, "display is Quartz Extreme accelerated" );

Eric Petit's avatar
Eric Petit committed
112 113 114 115 116 117 118 119 120
    p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
    if( p_vout->p_sys == NULL )
    {
        msg_Err( p_vout, "out of memory" );
        return( 1 );
    }

    memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );

121
    var_Get( p_vout->p_libvlc, "drawable", &value_drawable );
122
    if( value_drawable.i_int != 0 )
123
    {
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        static const GLint ATTRIBUTES[] = { 
            AGL_WINDOW,
            AGL_RGBA,
            AGL_NO_RECOVERY,
            AGL_ACCELERATED,
            AGL_DOUBLEBUFFER,
            AGL_RED_SIZE,   8,
            AGL_GREEN_SIZE, 8,
            AGL_BLUE_SIZE,  8,
            AGL_ALPHA_SIZE, 8,
            AGL_DEPTH_SIZE, 24,
            AGL_NONE };

        AGLPixelFormat pixFormat;

        p_vout->p_sys->b_embedded = VLC_TRUE;

141
        pixFormat = aglChoosePixelFormat(NULL, 0, ATTRIBUTES);
142 143 144 145 146 147 148 149 150 151 152 153 154 155
        if( NULL == pixFormat )
        {
            msg_Err( p_vout, "no screen renderer available for required attributes." );
            return VLC_EGENERIC;
        }
        
        p_vout->p_sys->agl_ctx = aglCreateContext(pixFormat, NULL);
        aglDestroyPixelFormat(pixFormat);
        if( NULL == p_vout->p_sys->agl_ctx )
        {
            msg_Err( p_vout, "cannot create AGL context." );
            return VLC_EGENERIC;
        }
        else {
156
            // tell opengl not to sync buffer swap with vertical retrace (too inefficient)
157
            GLint param = 0;
158 159 160 161 162 163 164
            aglSetInteger(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL, &param);
            aglEnable(p_vout->p_sys->agl_ctx, AGL_SWAP_INTERVAL);
        }

        p_vout->pf_init             = aglInit;
        p_vout->pf_end              = aglEnd;
        p_vout->pf_manage           = aglManage;
165
        p_vout->pf_control          = aglControl;
166
        p_vout->pf_swap             = aglSwap;
167 168
        p_vout->pf_lock             = aglLock;
        p_vout->pf_unlock           = aglUnlock;
169
    }
170 171
    else
    {
172 173
        NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];

174
        p_vout->p_sys->b_embedded = VLC_FALSE;
175

176
        [VLCGLView performSelectorOnMainThread:@selector(initVout:) withObject:[NSValue valueWithPointer:p_vout] waitUntilDone:YES];
177

178 179
        [o_pool release];

180
        /* Check to see if initVout: was successfull */
181

182
        if( !p_vout->p_sys->o_vout_view )
183 184 185
        {
            return VLC_EGENERIC;
        }
186

187 188 189 190 191 192 193 194 195
        p_vout->pf_init   = Init;
        p_vout->pf_end    = End;
        p_vout->pf_manage = Manage;
        p_vout->pf_control= Control;
        p_vout->pf_swap   = Swap;
        p_vout->pf_lock   = Lock;
        p_vout->pf_unlock = Unlock;
    }
    p_vout->p_sys->b_got_frame = VLC_FALSE;
196

Eric Petit's avatar
Eric Petit committed
197 198 199
    return VLC_SUCCESS;
}

200
void E_(CloseVideoGL) ( vlc_object_t * p_this )
Eric Petit's avatar
Eric Petit committed
201 202
{
    vout_thread_t * p_vout = (vout_thread_t *) p_this;
203 204 205 206
    if( p_vout->p_sys->b_embedded )
    {
        aglDestroyContext(p_vout->p_sys->agl_ctx);
    }
207
    else if(!VLCIntf->b_die)
208 209
    {
        NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
Eric Petit's avatar
Eric Petit committed
210

211
        /* Close the window */
212
        [p_vout->p_sys->o_vout_view performSelectorOnMainThread:@selector(closeVout) withObject:NULL waitUntilDone:YES];
Eric Petit's avatar
Eric Petit committed
213

214 215
        [o_pool release];
    }
216 217
    /* Clean up */
    free( p_vout->p_sys );
Eric Petit's avatar
Eric Petit committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
}

static int Init( vout_thread_t * p_vout )
{
    [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
    return VLC_SUCCESS;
}

static void End( vout_thread_t * p_vout )
{
    [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
}

static int Manage( vout_thread_t * p_vout )
{
233 234 235 236 237 238 239 240 241 242 243
    if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
    {
        [p_vout->p_sys->o_glview reshape];
        p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
    }
    if( p_vout->i_changes & VOUT_CROP_CHANGE )
    {
        [p_vout->p_sys->o_glview reshape];
        p_vout->i_changes &= ~VOUT_CROP_CHANGE;
    }

Eric Petit's avatar
Eric Petit committed
244 245 246
    if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
    {
        NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
247

Eric Petit's avatar
Eric Petit committed
248 249
        p_vout->b_fullscreen = !p_vout->b_fullscreen;

250 251
        if( p_vout->b_fullscreen )
            [p_vout->p_sys->o_vout_view enterFullscreen];
Eric Petit's avatar
Eric Petit committed
252
        else
253
            [p_vout->p_sys->o_vout_view leaveFullscreen];
254

Eric Petit's avatar
Eric Petit committed
255 256 257 258
        [o_pool release];

        p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
    }
259

260
    [p_vout->p_sys->o_vout_view manage];
Eric Petit's avatar
Eric Petit committed
261 262 263
    return VLC_SUCCESS;
}

264 265 266 267 268 269 270 271 272 273 274
/*****************************************************************************
 * Control: control facility for the vout
 *****************************************************************************/
static int Control( vout_thread_t *p_vout, int i_query, va_list args )
{
    vlc_bool_t b_arg;

    switch( i_query )
    {
        case VOUT_SET_STAY_ON_TOP:
            b_arg = va_arg( args, vlc_bool_t );
275
            [p_vout->p_sys->o_vout_view setOnTop: b_arg];
276 277 278 279 280 281 282 283 284
            return VLC_SUCCESS;

        case VOUT_CLOSE:
        case VOUT_REPARENT:
        default:
            return vout_vaControlDefault( p_vout, i_query, args );
    }
}

Eric Petit's avatar
Eric Petit committed
285 286
static void Swap( vout_thread_t * p_vout )
{
287
    p_vout->p_sys->b_got_frame = VLC_TRUE;
288
    [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
289 290 291 292
}

static int Lock( vout_thread_t * p_vout )
{
293 294
    if( kCGLNoError == CGLLockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]) )
    {
295 296
        [[p_vout->p_sys->o_glview openGLContext] makeCurrentContext];
        return 0;
297 298
    }
    return 1;
299 300 301 302
}

static void Unlock( vout_thread_t * p_vout )
{
303
    CGLUnlockContext([[p_vout->p_sys->o_glview openGLContext] CGLContextObj]);
Eric Petit's avatar
Eric Petit committed
304 305 306 307 308 309
}

/*****************************************************************************
 * VLCGLView implementation
 *****************************************************************************/
@implementation VLCGLView
310 311 312 313 314 315 316 317 318 319 320 321
+ (void)initVout:(NSValue *)arg
{
    vout_thread_t * p_vout = [arg pointerValue];

    /* Create the GL view */
    p_vout->p_sys->o_glview = [[VLCGLView alloc] initWithVout: p_vout];
    [p_vout->p_sys->o_glview autorelease];

    /* Spawn the window */
    p_vout->p_sys->o_vout_view = [VLCVoutView getVoutView: p_vout
                        subView: p_vout->p_sys->o_glview frame: nil];
}
Eric Petit's avatar
Eric Petit committed
322

323
/* This function will reset the o_vout_view. It's useful to go fullscreen. */
324
+ (void)resetVout:(NSData *)arg
325
{
326 327
    vout_thread_t * p_vout = [arg pointerValue];

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    if( p_vout->b_fullscreen )
    {
        /* Save window size and position */
        p_vout->p_sys->s_frame.size =
            [p_vout->p_sys->o_vout_view frame].size;
        p_vout->p_sys->s_frame.origin =
            [[p_vout->p_sys->o_vout_view getWindow ]frame].origin;
        p_vout->p_sys->b_saved_frame = VLC_TRUE;
    }

    [p_vout->p_sys->o_vout_view closeVout];

#define o_glview p_vout->p_sys->o_glview
    o_glview = [[VLCGLView alloc] initWithVout: p_vout];
    [o_glview autorelease];
    
    if( p_vout->p_sys->b_saved_frame )
    {
        p_vout->p_sys->o_vout_view = [VLCVoutView getVoutView: p_vout
                                                      subView: o_glview
                                                        frame: &p_vout->p_sys->s_frame];
    }
    else
    {
        p_vout->p_sys->o_vout_view = [VLCVoutView getVoutView: p_vout
                                                      subView: o_glview frame: nil];
        
    }
#undef o_glview
}

359
- (id) initWithVout: (vout_thread_t *) vout
Eric Petit's avatar
Eric Petit committed
360
{
361 362 363 364 365 366 367
    /* Must be called from main thread:
     * "The NSView class is generally thread-safe, with a few exceptions. You
     * should create, destroy, resize, move, and perform other operations on NSView
     * objects only from the main thread of an application. Drawing from secondary
     * threads is thread-safe as long as you bracket drawing calls with calls to
     * lockFocusIfCanDraw and unlockFocus." Cocoa Thread Safety */

368 369
    p_vout = vout;

370 371
    NSOpenGLPixelFormatAttribute attribs[] =
    {
372
        NSOpenGLPFADoubleBuffer,
373
        NSOpenGLPFAAccelerated,
374 375 376 377 378 379 380 381
        NSOpenGLPFANoRecovery,
        NSOpenGLPFAColorSize, 24,
        NSOpenGLPFAAlphaSize, 8,
        NSOpenGLPFADepthSize, 24,
        NSOpenGLPFAWindow,
        0
    };

382 383
    NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
        initWithAttributes: attribs];
Eric Petit's avatar
Eric Petit committed
384 385 386

    if( !fmt )
    {
387
        msg_Warn( p_vout, "could not create OpenGL video output" );
Eric Petit's avatar
Eric Petit committed
388 389 390
        return nil;
    }

391
    self = [super initWithFrame: NSMakeRect(0,0,10,10) pixelFormat: fmt];
Eric Petit's avatar
Eric Petit committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405
    [fmt release];

    [[self openGLContext] makeCurrentContext];
    [[self openGLContext] update];

    /* Swap buffers only during the vertical retrace of the monitor.
       http://developer.apple.com/documentation/GraphicsImaging/
       Conceptual/OpenGL/chap5/chapter_5_section_44.html */
    long params[] = { 1 };
    CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
                     params );
    return self;
}

406
- (void) reshape
Eric Petit's avatar
Eric Petit committed
407 408
{
    int x, y;
409
    vlc_value_t val;
410 411

    Lock( p_vout );
Eric Petit's avatar
Eric Petit committed
412
    NSRect bounds = [self bounds];
413 414 415 416 417 418 419

    var_Get( p_vout, "macosx-stretch", &val );
    if( val.b_bool )
    {
        x = bounds.size.width;
        y = bounds.size.height;
    }
420
    else if( bounds.size.height * p_vout->fmt_in.i_visible_width *
421
             p_vout->fmt_in.i_sar_num <
422 423
             bounds.size.width * p_vout->fmt_in.i_visible_height *
             p_vout->fmt_in.i_sar_den )
Eric Petit's avatar
Eric Petit committed
424
    {
425 426 427 428
        x = ( bounds.size.height * p_vout->fmt_in.i_visible_width *
              p_vout->fmt_in.i_sar_num ) /
            ( p_vout->fmt_in.i_visible_height * p_vout->fmt_in.i_sar_den);

Eric Petit's avatar
Eric Petit committed
429 430 431 432 433
        y = bounds.size.height;
    }
    else
    {
        x = bounds.size.width;
434 435 436
        y = ( bounds.size.width * p_vout->fmt_in.i_visible_height *
              p_vout->fmt_in.i_sar_den) /
            ( p_vout->fmt_in.i_visible_width * p_vout->fmt_in.i_sar_num  );
Eric Petit's avatar
Eric Petit committed
437
    }
438

Eric Petit's avatar
Eric Petit committed
439 440
    glViewport( ( bounds.size.width - x ) / 2,
                ( bounds.size.height - y ) / 2, x, y );
441

442 443
    [super reshape];

444 445 446 447 448
    if( p_vout->p_sys->b_got_frame )
    {
        /* Ask the opengl module to redraw */
        vout_thread_t * p_parent;
        p_parent = (vout_thread_t *) p_vout->p_parent;
449
        Unlock( p_vout );
450 451 452 453 454 455 456 457
        if( p_parent && p_parent->pf_display )
        {
            p_parent->pf_display( p_parent, NULL );
        }
    }
    else
    {
        glClear( GL_COLOR_BUFFER_BIT );
458
        Unlock( p_vout );
459
    }
460 461 462 463 464 465 466
}

- (void) update
{
    Lock( p_vout );
    [super update];
    Unlock( p_vout );
Eric Petit's avatar
Eric Petit committed
467 468 469 470
}

- (void) drawRect: (NSRect) rect
{
471
    Lock( p_vout );
472
    [[p_vout->p_sys->o_glview openGLContext] flushBuffer];
473
    [super drawRect:rect];
474
    Unlock( p_vout );
Eric Petit's avatar
Eric Petit committed
475 476 477 478
}

@end

479 480 481 482
/*****************************************************************************
 * embedded AGL context implementation
 *****************************************************************************/

483
static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds );
484
static void aglReshape( vout_thread_t * p_vout );
485
static OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
486 487 488

static int aglInit( vout_thread_t * p_vout )
{
489 490 491 492 493
    vlc_value_t val;

    Rect viewBounds;    
    Rect clipBounds;
    
494
    var_Get( p_vout->p_libvlc, "drawable", &val );
495 496 497
    p_vout->p_sys->agl_drawable = (AGLDrawable)val.i_int;
    aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);

498
    var_Get( p_vout->p_libvlc, "drawable-view-top", &val );
499
    viewBounds.top = val.i_int;
500
    var_Get( p_vout->p_libvlc, "drawable-view-left", &val );
501
    viewBounds.left = val.i_int;
502
    var_Get( p_vout->p_libvlc, "drawable-view-bottom", &val );
503
    viewBounds.bottom = val.i_int;
504
    var_Get( p_vout->p_libvlc, "drawable-view-right", &val );
505
    viewBounds.right = val.i_int;
506
    var_Get( p_vout->p_libvlc, "drawable-clip-top", &val );
507
    clipBounds.top = val.i_int;
508
    var_Get( p_vout->p_libvlc, "drawable-clip-left", &val );
509
    clipBounds.left = val.i_int;
510
    var_Get( p_vout->p_libvlc, "drawable-clip-bottom", &val );
511
    clipBounds.bottom = val.i_int;
512
    var_Get( p_vout->p_libvlc, "drawable-clip-right", &val );
513 514
    clipBounds.right = val.i_int;

515 516 517 518 519 520 521 522 523 524 525 526
    p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
                                 || (clipBounds.left == clipBounds.right);
    if( ! p_vout->p_sys->b_clipped_out )
    {
        aglLock(p_vout);
        aglSetViewport(p_vout, viewBounds, clipBounds);
        aglReshape(p_vout);
        aglUnlock(p_vout);
    }
    p_vout->p_sys->clipBounds = clipBounds;
    p_vout->p_sys->viewBounds = viewBounds;

527 528 529 530 531
    return VLC_SUCCESS;
}

static void aglEnd( vout_thread_t * p_vout )
{
532
    aglSetCurrentContext(NULL);
533
    if( p_vout->p_sys->theWindow ) DisposeWindow( p_vout->p_sys->theWindow );
534 535 536 537
}

static void aglReshape( vout_thread_t * p_vout )
{
538 539 540
    unsigned int x, y;
    unsigned int i_height = p_vout->p_sys->i_height;
    unsigned int i_width  = p_vout->p_sys->i_width;
541

542
    vout_PlacePicture(p_vout, i_width, i_height, &x, &y, &i_width, &i_height); 
543

544
    glViewport( p_vout->p_sys->i_offx + x, p_vout->p_sys->i_offy + y, i_width, i_height );
545 546 547 548 549 550 551 552 553 554 555 556 557 558

    if( p_vout->p_sys->b_got_frame )
    {
        /* Ask the opengl module to redraw */
        vout_thread_t * p_parent;
        p_parent = (vout_thread_t *) p_vout->p_parent;
        if( p_parent && p_parent->pf_display )
        {
            p_parent->pf_display( p_parent, NULL );
        }
    }
    else
    {
        glClear( GL_COLOR_BUFFER_BIT );
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
    }
}

/* private event class */
enum 
{
    kEventClassVLCPlugin = 'vlcp',
};
/* private event kinds */
enum
{
    kEventVLCPluginShowFullscreen = 32768,
    kEventVLCPluginHideFullscreen,
};

static void sendEventToMainThread(EventTargetRef target, UInt32 class, UInt32 kind)
{
    EventRef myEvent;
    if( noErr == CreateEvent(NULL, class, kind, 0, kEventAttributeNone, &myEvent) )
    {
        if( noErr == SetEventParameter(myEvent, kEventParamPostTarget, typeEventTargetRef, sizeof(EventTargetRef), &target) )
        {
            PostEventToQueue(GetMainEventQueue(), myEvent, kEventPriorityStandard);
        }
        ReleaseEvent(myEvent);
584 585 586 587 588 589 590
    }
}

static int aglManage( vout_thread_t * p_vout )
{
    if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
    {
591
        aglLock( p_vout );
592
        aglReshape(p_vout);
593
        aglUnlock( p_vout );
594 595 596 597
        p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
    }
    if( p_vout->i_changes & VOUT_CROP_CHANGE )
    {
598
        aglLock( p_vout );
599
        aglReshape(p_vout);
600
        aglUnlock( p_vout );
601 602
        p_vout->i_changes &= ~VOUT_CROP_CHANGE;
    }
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
    if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
    {
        aglSetDrawable(p_vout->p_sys->agl_ctx, NULL);
        aglLock( p_vout );
        if( p_vout->b_fullscreen )
        {
            /* Close the fullscreen window and resume normal drawing */
            vlc_value_t val;
            Rect viewBounds;    
            Rect clipBounds;

            var_Get( p_vout->p_libvlc, "drawable", &val );
            p_vout->p_sys->agl_drawable = (AGLDrawable)val.i_int;
            aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);

            var_Get( p_vout->p_libvlc, "drawable-view-top", &val );
            viewBounds.top = val.i_int;
            var_Get( p_vout->p_libvlc, "drawable-view-left", &val );
            viewBounds.left = val.i_int;
            var_Get( p_vout->p_libvlc, "drawable-view-bottom", &val );
            viewBounds.bottom = val.i_int;
            var_Get( p_vout->p_libvlc, "drawable-view-right", &val );
            viewBounds.right = val.i_int;
            var_Get( p_vout->p_libvlc, "drawable-clip-top", &val );
            clipBounds.top = val.i_int;
            var_Get( p_vout->p_libvlc, "drawable-clip-left", &val );
            clipBounds.left = val.i_int;
            var_Get( p_vout->p_libvlc, "drawable-clip-bottom", &val );
            clipBounds.bottom = val.i_int;
            var_Get( p_vout->p_libvlc, "drawable-clip-right", &val );
            clipBounds.right = val.i_int;

            aglSetCurrentContext(p_vout->p_sys->agl_ctx);
            aglSetViewport(p_vout, viewBounds, clipBounds);

            /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty update to the main thread */
            sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow), kEventClassVLCPlugin, kEventVLCPluginHideFullscreen);
        }
        else
        {
            Rect deviceRect;
            
            GDHandle deviceHdl = GetMainDevice();
            deviceRect = (*deviceHdl)->gdRect;
            
            if( !p_vout->p_sys->theWindow )
            {
                /* Create a window */
                WindowAttributes    windowAttrs;

                windowAttrs = kWindowStandardDocumentAttributes
                            | kWindowStandardHandlerAttribute
                            | kWindowLiveResizeAttribute
                            | kWindowNoShadowAttribute;
                                            
                windowAttrs &= (~kWindowResizableAttribute);

                CreateNewWindow(kDocumentWindowClass, windowAttrs, &deviceRect, &p_vout->p_sys->theWindow);
                if( !p_vout->p_sys->winGroup )
                {
                    CreateWindowGroup(0, &p_vout->p_sys->winGroup);
                    SetWindowGroup(p_vout->p_sys->theWindow, p_vout->p_sys->winGroup);
                    SetWindowGroupParent( p_vout->p_sys->winGroup, GetWindowGroupOfClass(kDocumentWindowClass) ) ;
                }
                
                // Window title
                CFStringRef titleKey    = CFSTR("Fullscreen VLC media plugin");
                CFStringRef windowTitle = CFCopyLocalizedString(titleKey, NULL);
                SetWindowTitleWithCFString(p_vout->p_sys->theWindow, windowTitle);
                CFRelease(titleKey);
                CFRelease(windowTitle);
                
                //Install event handler
                static const EventTypeSpec win_events[] = {
                    { kEventClassMouse, kEventMouseDown },
                    { kEventClassMouse, kEventMouseMoved },
                    { kEventClassMouse, kEventMouseUp },
                    { kEventClassWindow, kEventWindowClosed },
                    { kEventClassWindow, kEventWindowBoundsChanged },
                    { kEventClassCommand, kEventCommandProcess },
                    { kEventClassVLCPlugin, kEventVLCPluginShowFullscreen },
                    { kEventClassVLCPlugin, kEventVLCPluginHideFullscreen },
                };
                InstallWindowEventHandler (p_vout->p_sys->theWindow, NewEventHandlerUPP (WindowEventHandler), GetEventTypeCount(win_events), win_events, p_vout, NULL);
            }
            else
            {
                /* just in case device resolution changed */
                SetWindowBounds(p_vout->p_sys->theWindow, kWindowContentRgn, &deviceRect);
            }
            glClear( GL_COLOR_BUFFER_BIT );
            p_vout->p_sys->agl_drawable = (AGLDrawable)GetWindowPort(p_vout->p_sys->theWindow);
            aglSetDrawable(p_vout->p_sys->agl_ctx, p_vout->p_sys->agl_drawable);
            aglSetCurrentContext(p_vout->p_sys->agl_ctx);
            aglSetViewport(p_vout, deviceRect, deviceRect);
            //aglSetFullScreen(p_vout->p_sys->agl_ctx, device_width, device_height, 0, 0);

            /* Most Carbon APIs are not thread-safe, therefore delagate some GUI visibilty update to the main thread */
            sendEventToMainThread(GetWindowEventTarget(p_vout->p_sys->theWindow), kEventClassVLCPlugin, kEventVLCPluginShowFullscreen);
        }
        aglReshape(p_vout);
        aglUnlock( p_vout );
        p_vout->b_fullscreen = !p_vout->b_fullscreen;
        p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
    }
708 709 710
    return VLC_SUCCESS;
}

711 712 713 714 715
static int aglControl( vout_thread_t *p_vout, int i_query, va_list args )
{
    switch( i_query )
    {
        case VOUT_SET_VIEWPORT:
716 717
        {
            Rect viewBounds, clipBounds;
718 719 720 721 722 723 724 725
            viewBounds.top = va_arg( args, int);
            viewBounds.left = va_arg( args, int);
            viewBounds.bottom = va_arg( args, int);
            viewBounds.right = va_arg( args, int);
            clipBounds.top = va_arg( args, int);
            clipBounds.left = va_arg( args, int);
            clipBounds.bottom = va_arg( args, int);
            clipBounds.right = va_arg( args, int);
726 727 728
            
            if( !p_vout->b_fullscreen ) 
            {
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
                /*
                ** check that the clip rect is not empty, as this is used
                ** by Firefox to prevent a plugin from displaying during
                ** a scrolling event. In this case we just prevent buffers
                ** from being swapped and ignore clipping as this is less
                ** disruptive than a GL geometry change
                */

                p_vout->p_sys->b_clipped_out = (clipBounds.top == clipBounds.bottom)
                                             || (clipBounds.left == clipBounds.right);
                if( ! p_vout->p_sys->b_clipped_out )
                {
                    /* ignore consecutive viewport update with identical parameters */
                    if( memcmp(&clipBounds, &(p_vout->p_sys->clipBounds), sizeof(clipBounds) )
                     && memcmp(&viewBounds, &(p_vout->p_sys->viewBounds), sizeof(viewBounds)) )
                    {
                        aglLock( p_vout );
                        aglSetViewport(p_vout, viewBounds, clipBounds);
                        aglReshape( p_vout );
                        aglUnlock( p_vout );
                        p_vout->p_sys->clipBounds = clipBounds;
                        p_vout->p_sys->viewBounds = viewBounds;
                    }
                }
            }
            return VLC_SUCCESS;
        }

        case VOUT_REDRAW_RECT:
        {
            vout_thread_t * p_parent;
            Rect areaBounds;

            areaBounds.top = va_arg( args, int);
            areaBounds.left = va_arg( args, int);
            areaBounds.bottom = va_arg( args, int);
            areaBounds.right = va_arg( args, int);

            /* Ask the opengl module to redraw */
            p_parent = (vout_thread_t *) p_vout->p_parent;
            if( p_parent && p_parent->pf_display )
            {
                p_parent->pf_display( p_parent, NULL );
772
            }
773
            return VLC_SUCCESS;
774
        }
775 776

        case VOUT_REPARENT:
777 778 779 780 781 782 783
        {
            AGLDrawable drawable = (AGLDrawable)va_arg( args, int);
            if( !p_vout->b_fullscreen && drawable != p_vout->p_sys->agl_drawable )
            {
                p_vout->p_sys->agl_drawable = drawable;
                aglSetDrawable(p_vout->p_sys->agl_ctx, drawable);
            }
784
            return VLC_SUCCESS;
785
        }
786 787 788 789 790 791

        default:
            return vout_vaControlDefault( p_vout, i_query, args );
    }
}

792 793
static void aglSwap( vout_thread_t * p_vout )
{
794 795 796 797 798 799 800 801 802 803
    if( ! p_vout->p_sys->b_clipped_out )
    {
        p_vout->p_sys->b_got_frame = VLC_TRUE;
        aglSwapBuffers(p_vout->p_sys->agl_ctx);
    }
    else
    {
        /* drop frame */
        glFlush();
    }
804 805
}

806
/* Enter this function with the p_vout locked */
807
static void aglSetViewport( vout_thread_t *p_vout, Rect viewBounds, Rect clipBounds )
808 809 810 811
{
    // mozilla plugin provides coordinates based on port bounds
    // however AGL coordinates are based on window structure region
    // and are vertically flipped
812 813 814
    GLint rect[4];
    CGrafPtr port = (CGrafPtr)p_vout->p_sys->agl_drawable;
    Rect winBounds, clientBounds;
815

816
    GetWindowBounds(GetWindowFromPort(port),
817
        kWindowStructureRgn, &winBounds);
818
    GetWindowBounds(GetWindowFromPort(port),
819 820 821 822
        kWindowContentRgn, &clientBounds);

    /* update video clipping bounds in drawable */
    rect[0] = (clientBounds.left-winBounds.left)
823
            + clipBounds.left;                  // from window left edge
824 825
    rect[1] = (winBounds.bottom-winBounds.top)
            - (clientBounds.top-winBounds.top)
826 827 828
            - clipBounds.bottom;                // from window bottom edge
    rect[2] = clipBounds.right-clipBounds.left; // width
    rect[3] = clipBounds.bottom-clipBounds.top; // height
829 830 831 832
    aglSetInteger(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT, rect);
    aglEnable(p_vout->p_sys->agl_ctx, AGL_BUFFER_RECT);

    /* update video internal bounds in drawable */
833 834 835 836 837
    p_vout->p_sys->i_width  = viewBounds.right-viewBounds.left;
    p_vout->p_sys->i_height = viewBounds.bottom-viewBounds.top;
    p_vout->p_sys->i_offx   = -clipBounds.left - viewBounds.left;
    p_vout->p_sys->i_offy   = clipBounds.bottom + viewBounds.top
                            - p_vout->p_sys->i_height; 
838

839
    aglUpdateContext(p_vout->p_sys->agl_ctx);
840 841
}

842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
//default window event handler
static pascal OSStatus WindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData)
{
    OSStatus result = noErr;
    UInt32 class = GetEventClass (event);
    UInt32 kind = GetEventKind (event); 
    vout_thread_t *p_vout = (vout_thread_t *)userData;

    result = CallNextEventHandler(nextHandler, event);
    if(class == kEventClassCommand)
    {
        HICommand theHICommand;
        GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL, sizeof( HICommand ), NULL, &theHICommand );
        
        switch ( theHICommand.commandID )
        {
            default:
                result = eventNotHandledErr;
        }
    }
    else if(class == kEventClassWindow)
    {
        WindowRef     window;
        Rect          rectPort = {0,0,0,0};
        
        GetEventParameter(event, kEventParamDirectObject, typeWindowRef, NULL, sizeof(WindowRef), NULL, &window);

        if(window)
        {
            GetPortBounds(GetWindowPort(window), &rectPort);
        }   

        switch (kind)
        {
            case kEventWindowClosed:
            case kEventWindowZoomed:
            case kEventWindowBoundsChanged:
                break;
            
            default:
                result = eventNotHandledErr;
        }
    }
    else if(class == kEventClassMouse)
    {
        switch (kind)
        {
            case kEventMouseDown:
            {
                UInt16     button;
        
                GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
                switch (button)
                {
                    case kEventMouseButtonPrimary:
                    {
                        vlc_value_t val;

                        var_Get( p_vout, "mouse-button-down", &val );
                        val.i_int |= 1;
                        var_Set( p_vout, "mouse-button-down", val );
                        break;
                    }
                    case kEventMouseButtonSecondary:
                    {
                        vlc_value_t val;

                        var_Get( p_vout, "mouse-button-down", &val );
                        val.i_int |= 2;
                        var_Set( p_vout, "mouse-button-down", val );
                        break;
                    }
                    case kEventMouseButtonTertiary:
                    {
                        vlc_value_t val;

                        var_Get( p_vout, "mouse-button-down", &val );
                        val.i_int |= 4;
                        var_Set( p_vout, "mouse-button-down", val );
                        break;
                    }
                    default:
                        result = eventNotHandledErr;
                }
                break;
            }

            case kEventMouseUp:
            {
                UInt16     button;
        
                GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(button), NULL, &button);
                switch (button)
                {
                    case kEventMouseButtonPrimary:
                    {
                        UInt32 clickCount = 0;
                        GetEventParameter(event, kEventParamClickCount, typeUInt32, NULL, sizeof(clickCount), NULL, &clickCount);
                        if( clickCount > 1 )
                        {
                            vlc_value_t val;

                            val.b_bool = VLC_FALSE;
                            var_Set((vout_thread_t *) p_vout->p_parent, "fullscreen", val);
                        }
                        else
                        {
                            vlc_value_t val;

                            val.b_bool = VLC_TRUE;
                            var_Set( p_vout, "mouse-clicked", val );

                            var_Get( p_vout, "mouse-button-down", &val );
                            val.i_int &= ~1;
                            var_Set( p_vout, "mouse-button-down", val );
                        }
                        break;
                    }
                    case kEventMouseButtonSecondary:
                    {
                        vlc_value_t val;

                        var_Get( p_vout, "mouse-button-down", &val );
                        val.i_int &= ~2;
                        var_Set( p_vout, "mouse-button-down", val );
                        break;
                    }
                    case kEventMouseButtonTertiary:
                    {
                        vlc_value_t val;

                        var_Get( p_vout, "mouse-button-down", &val );
                        val.i_int &= ~2;
                        var_Set( p_vout, "mouse-button-down", val );
                        break;
                    }
                    default:
                        result = eventNotHandledErr;
                }
                break;
            }

            case kEventMouseMoved:
            {
                Point ml;
                vlc_value_t val;

                unsigned int i_x, i_y;
                unsigned int i_height = p_vout->p_sys->i_height;
                unsigned int i_width  = p_vout->p_sys->i_width;

                vout_PlacePicture(p_vout, i_width, i_height, &i_x, &i_y, &i_width, &i_height); 

                GetEventParameter(event, kEventParamWindowMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &ml);
                
                val.i_int = ( ((int)ml.h) - i_x ) *
                            p_vout->render.i_width / i_width;
                var_Set( p_vout, "mouse-x", val );

                val.i_int = ( ((int)ml.v) - i_y ) *
                            p_vout->render.i_height / i_height;

                var_Set( p_vout, "mouse-y", val );

                val.b_bool = VLC_TRUE;
                var_Set( p_vout, "mouse-moved", val );

                break;
            }
            
            default:
                result = eventNotHandledErr;
        }
    }
    else if(class == kEventClassTextInput)
    {
        switch (kind)
        {
            case kEventTextInputUnicodeForKeyEvent:
            {
                break;
            }
            default:
                result = eventNotHandledErr;
        }
    }
    else if(class == kEventClassVLCPlugin)
    {
        switch (kind)
        {
            case kEventVLCPluginShowFullscreen:
                ShowWindow (p_vout->p_sys->theWindow);
                SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
                //CGDisplayHideCursor(kCGDirectMainDisplay);
                break;
            case kEventVLCPluginHideFullscreen:
                HideWindow (p_vout->p_sys->theWindow);
                SetSystemUIMode( kUIModeNormal, 0);
                CGDisplayShowCursor(kCGDirectMainDisplay);
                break;
            default:
                result = eventNotHandledErr;
                break;
        }
    }
    return result;
}

static int aglLock( vout_thread_t * p_vout )
{
#ifdef __ppc__
    /*
     * before 10.4, we set the AGL context as current and
     * then we retrieve and use the matching CGL context 
     */
    aglSetCurrentContext(p_vout->p_sys->agl_ctx);
    return kCGLNoError != CGLLockContext( CGLGetCurrentContext() );
#else
    /* since 10.4, this is the safe way to get the underlying CGL context */
    CGLContextObj cglContext;
    if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
    {
        if( kCGLNoError == CGLLockContext( cglContext ) )
1065 1066 1067 1068
        {
            aglSetCurrentContext(p_vout->p_sys->agl_ctx);
            return 0;
        }
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
    }
    return 1;
#endif
}

static void aglUnlock( vout_thread_t * p_vout )
{
#ifdef __ppc__
    /*
     * before 10.4, we assume that the AGL context is current.
     * therefore, we use the current CGL context 
     */
    CGLUnlockContext( CGLGetCurrentContext() );
#else
    /* since 10.4, this is the safe way to get the underlying CGL context */
    CGLContextObj cglContext;
    if( aglGetCGLContext(p_vout->p_sys->agl_ctx, (void**)&cglContext) )
    {
        CGLUnlockContext( cglContext );
    }
#endif
}