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