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