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