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