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