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