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