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