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