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