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