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