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