]> git.sesse.net Git - vlc/blob - modules/video_output/macosx.m
demux: fix path extension on non-local inputs (fixes #8115)
[vlc] / modules / video_output / macosx.m
1 /*****************************************************************************
2  * macosx.m: MacOS X OpenGL provider
3  *****************************************************************************
4  * Copyright (C) 2001-2013 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8  *          Eric Petit <titer@m0k.org>
9  *          Benjamin Pracht <bigben at videolan dot org>
10  *          Damien Fouilleul <damienf at videolan dot org>
11  *          Pierre d'Herbemont <pdherbemont at videolan dot org>
12  *          Felix Paul Kühne <fkuehne at videolan dot org>
13  *          David Fuhrmann <david dot fuhrmann at googlemail dot com>
14  *          Rémi Denis-Courmont
15  *          Juho Vähä-Herttua <juhovh at iki dot fi>
16  *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
17  *
18  * This program is free software; you can redistribute it and/or modify it
19  * under the terms of the GNU Lesser General Public License as published by
20  * the Free Software Foundation; either version 2.1 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License
29  * along with this program; if not, write to the Free Software Foundation,
30  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
31  *****************************************************************************/
32
33 /*****************************************************************************
34  * Preamble
35  *****************************************************************************/
36
37 #import <Cocoa/Cocoa.h>
38 #import <OpenGL/OpenGL.h>
39 #import <dlfcn.h>
40
41 #ifdef HAVE_CONFIG_H
42 # include "config.h"
43 #endif
44
45 #include <vlc_common.h>
46 #include <vlc_plugin.h>
47 #include <vlc_vout_display.h>
48 #include <vlc_opengl.h>
49 #include <vlc_dialog.h>
50 #include "opengl.h"
51
52 /* compilation support for 10.5 and 10.6 */
53 #define OSX_LION NSAppKitVersionNumber >= 1115.2
54 #ifndef MAC_OS_X_VERSION_10_7
55
56 @interface NSView (IntroducedInLion)
57 - (NSRect)convertRectToBacking:(NSRect)aRect;
58 - (void)setWantsBestResolutionOpenGLSurface:(BOOL)aBool;
59 @end
60
61 #endif
62
63 /**
64  * Forward declarations
65  */
66 static int Open (vlc_object_t *);
67 static void Close (vlc_object_t *);
68
69 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count);
70 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture);
71 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture);
72 static int Control (vout_display_t *vd, int query, va_list ap);
73
74 static void *OurGetProcAddress(vlc_gl_t *, const char *);
75
76 static int OpenglLock (vlc_gl_t *gl);
77 static void OpenglUnlock (vlc_gl_t *gl);
78 static void OpenglSwap (vlc_gl_t *gl);
79
80 /**
81  * Module declaration
82  */
83 vlc_module_begin ()
84     /* Will be loaded even without interface module. see voutgl.m */
85     set_shortname ("Mac OS X")
86     set_description (N_("Mac OS X OpenGL video output (requires drawable-nsobject)"))
87     set_category (CAT_VIDEO)
88     set_subcategory (SUBCAT_VIDEO_VOUT)
89     set_capability ("vout display", 300)
90     set_callbacks (Open, Close)
91
92     add_shortcut ("macosx", "vout_macosx")
93 vlc_module_end ()
94
95 /**
96  * Obj-C protocol declaration that drawable-nsobject should follow
97  */
98 @protocol VLCOpenGLVideoViewEmbedding <NSObject>
99 - (void)addVoutSubview:(NSView *)view;
100 - (void)removeVoutSubview:(NSView *)view;
101 @end
102
103 @interface VLCOpenGLVideoView : NSOpenGLView
104 {
105     vout_display_t *vd;
106     BOOL _hasPendingReshape;
107 }
108 - (void)setVoutDisplay:(vout_display_t *)vd;
109 - (void)setVoutFlushing:(BOOL)flushing;
110 @end
111
112
113 struct vout_display_sys_t
114 {
115     VLCOpenGLVideoView *glView;
116     id<VLCOpenGLVideoViewEmbedding> container;
117
118     vout_window_t *embed;
119     vlc_gl_t gl;
120     vout_display_opengl_t *vgl;
121
122     picture_pool_t *pool;
123     picture_t *current;
124     bool has_first_frame;
125
126     vout_display_place_t place;
127 };
128
129
130 static void *OurGetProcAddress(vlc_gl_t *gl, const char *name)
131 {
132     VLC_UNUSED(gl);
133
134     return dlsym(RTLD_DEFAULT, name);
135 }
136
137 static int Open (vlc_object_t *this)
138 {
139     vout_display_t *vd = (vout_display_t *)this;
140     vout_display_sys_t *sys = calloc (1, sizeof(*sys));
141     NSAutoreleasePool *nsPool = nil;
142
143     if (!sys)
144         return VLC_ENOMEM;
145
146     if (!CGDisplayUsesOpenGLAcceleration (kCGDirectMainDisplay)) {
147         msg_Err (this, "no OpenGL hardware acceleration found. this can lead to slow output and unexpected results");
148         dialog_Fatal (this, _("OpenGL acceleration is not supported on your Mac"), _("Your Mac lacks Quartz Extreme acceleration, which is required for video output. It will still work, but much slower and with possibly unexpected results."));
149     } else
150         msg_Dbg (this, "Quartz Extreme acceleration is active");
151
152     vd->sys = sys;
153     sys->pool = NULL;
154     sys->gl.sys = NULL;
155     sys->embed = NULL;
156
157     /* Get the drawable object */
158     id container = var_CreateGetAddress (vd, "drawable-nsobject");
159     if (container)
160         vout_display_DeleteWindow (vd, NULL);
161     else {
162         vout_window_cfg_t wnd_cfg;
163
164         memset (&wnd_cfg, 0, sizeof (wnd_cfg));
165         wnd_cfg.type = VOUT_WINDOW_TYPE_NSOBJECT;
166         wnd_cfg.x = var_InheritInteger (vd, "video-x");
167         wnd_cfg.y = var_InheritInteger (vd, "video-y");
168         wnd_cfg.width  = vd->cfg->display.width;
169         wnd_cfg.height = vd->cfg->display.height;
170
171         sys->embed = vout_display_NewWindow (vd, &wnd_cfg);
172         if (sys->embed)
173             container = sys->embed->handle.nsobject;
174
175         if (!container) {
176             msg_Dbg(vd, "No drawable-nsobject nor vout_window_t found, passing over.");
177             goto error;
178         }
179     }
180
181     /* This will be released in Close(), on
182      * main thread, after we are done using it. */
183     sys->container = [container retain];
184
185     /* Get our main view*/
186     nsPool = [[NSAutoreleasePool alloc] init];
187
188     [VLCOpenGLVideoView performSelectorOnMainThread:@selector(getNewView:) withObject:[NSValue valueWithPointer:&sys->glView] waitUntilDone:YES];
189     if (!sys->glView)
190         goto error;
191
192     [sys->glView setVoutDisplay:vd];
193
194     /* We don't wait, that means that we'll have to be careful about releasing
195      * container.
196      * That's why we'll release on main thread in Close(). */
197     if ([(id)container respondsToSelector:@selector(addVoutSubview:)])
198         [(id)container performSelectorOnMainThread:@selector(addVoutSubview:) withObject:sys->glView waitUntilDone:NO];
199     else if ([container isKindOfClass:[NSView class]]) {
200         NSView *parentView = container;
201         [parentView performSelectorOnMainThread:@selector(addSubview:) withObject:sys->glView waitUntilDone:NO];
202         [sys->glView performSelectorOnMainThread:@selector(setFrameToBoundsOfView:) withObject:[NSValue valueWithPointer:parentView] waitUntilDone:NO];
203     } else {
204         msg_Err(vd, "Invalid drawable-nsobject object. drawable-nsobject must either be an NSView or comply to the @protocol VLCOpenGLVideoViewEmbedding.");
205         goto error;
206     }
207
208
209     [nsPool release];
210     nsPool = nil;
211
212     /* Initialize common OpenGL video display */
213     sys->gl.lock = OpenglLock;
214     sys->gl.unlock = OpenglUnlock;
215     sys->gl.swap = OpenglSwap;
216     sys->gl.getProcAddress = OurGetProcAddress;
217     sys->gl.sys = sys;
218     const vlc_fourcc_t *subpicture_chromas;
219     video_format_t fmt = vd->fmt;
220
221     sys->vgl = vout_display_opengl_New (&vd->fmt, &subpicture_chromas, &sys->gl);
222     if (!sys->vgl) {
223         sys->gl.sys = NULL;
224         goto error;
225     }
226
227     /* */
228     vout_display_info_t info = vd->info;
229     info.has_pictures_invalid = false;
230     info.has_event_thread = true;
231     info.subpicture_chromas = subpicture_chromas;
232     info.has_hide_mouse = true;
233
234     /* Setup vout_display_t once everything is fine */
235     vd->info = info;
236
237     vd->pool = Pool;
238     vd->prepare = PictureRender;
239     vd->display = PictureDisplay;
240     vd->control = Control;
241
242     /* */
243     vout_display_SendEventDisplaySize (vd, vd->source.i_visible_width, vd->source.i_visible_height, false);
244
245     return VLC_SUCCESS;
246
247 error:
248     [nsPool release];
249     Close(this);
250     return VLC_EGENERIC;
251 }
252
253 void Close (vlc_object_t *this)
254 {
255     vout_display_t *vd = (vout_display_t *)this;
256     vout_display_sys_t *sys = vd->sys;
257
258     [sys->glView setVoutDisplay:nil];
259
260     var_Destroy (vd, "drawable-nsobject");
261     if ([(id)sys->container respondsToSelector:@selector(removeVoutSubview:)])
262         /* This will retain sys->glView */
263         [(id)sys->container performSelectorOnMainThread:@selector(removeVoutSubview:) withObject:sys->glView waitUntilDone:NO];
264
265     /* release on main thread as explained in Open() */
266     [(id)sys->container performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
267     [sys->glView performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
268
269     if (sys->gl.sys != NULL)
270         vout_display_opengl_Delete (sys->vgl);
271
272     [sys->glView release];
273
274     if (sys->embed)
275         vout_display_DeleteWindow (vd, sys->embed);
276     free (sys);
277 }
278
279 /*****************************************************************************
280  * vout display callbacks
281  *****************************************************************************/
282
283 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
284 {
285     vout_display_sys_t *sys = vd->sys;
286
287     if (!sys->pool)
288         sys->pool = vout_display_opengl_GetPool (sys->vgl, requested_count);
289     assert(sys->pool);
290     return sys->pool;
291 }
292
293 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
294 {
295
296     vout_display_sys_t *sys = vd->sys;
297
298     vout_display_opengl_Prepare (sys->vgl, pic, subpicture);
299 }
300
301 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
302 {
303     vout_display_sys_t *sys = vd->sys;
304     [sys->glView setVoutFlushing:YES];
305     vout_display_opengl_Display (sys->vgl, &vd->source);
306     [sys->glView setVoutFlushing:NO];
307     picture_Release (pic);
308     sys->has_first_frame = true;
309
310     if (subpicture)
311         subpicture_Delete(subpicture);
312 }
313
314 static int Control (vout_display_t *vd, int query, va_list ap)
315 {
316     vout_display_sys_t *sys = vd->sys;
317
318     if (!vd->sys)
319         return VLC_EGENERIC;
320
321     if (!sys->embed)
322         return VLC_EGENERIC;
323
324     switch (query)
325     {
326         case VOUT_DISPLAY_CHANGE_FULLSCREEN:
327         {
328             const vout_display_cfg_t *cfg = va_arg (ap, const vout_display_cfg_t *);
329             if (vout_window_SetFullScreen (sys->embed, cfg->is_fullscreen))
330                 return VLC_EGENERIC;
331
332             return VLC_SUCCESS;
333         }
334         case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
335         {
336             unsigned state = va_arg (ap, unsigned);
337             return vout_window_SetState (sys->embed, state);            
338         }
339         case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
340         case VOUT_DISPLAY_CHANGE_ZOOM:
341         case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
342         case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
343         case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
344         {
345             NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
346
347             id o_window = [sys->glView window];
348             if (!o_window)
349                 return VLC_SUCCESS; // this is okay, since the event will occur again when we have a window
350
351             NSSize windowMinSize = [o_window minSize];
352
353             const vout_display_cfg_t *cfg;
354             const video_format_t *source;
355             bool is_forced = false;
356
357             if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP) {
358                 source = (const video_format_t *)va_arg (ap, const video_format_t *);
359                 cfg = vd->cfg;
360             } else {
361                 source = &vd->source;
362                 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
363                 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
364                     is_forced = (bool)va_arg (ap, int);
365             }
366
367             if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE && is_forced
368                 && (cfg->display.width != vd->cfg->display.width
369                     || cfg->display.height != vd->cfg->display.height)
370                 && vout_window_SetSize (sys->embed, cfg->display.width, cfg->display.height))
371                 return VLC_EGENERIC;
372  
373             /* we always use our current frame here, because we have some size constraints 
374                in the ui vout provider */
375             vout_display_cfg_t cfg_tmp = *cfg;
376             NSRect bounds;
377             /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
378             if (OSX_LION)
379                 bounds = [sys->glView convertRectToBacking:[sys->glView bounds]];
380             else
381                 bounds = [sys->glView bounds];
382             cfg_tmp.display.width = bounds.size.width;
383             cfg_tmp.display.height = bounds.size.height;
384
385             vout_display_place_t place;
386             vout_display_PlacePicture (&place, source, &cfg_tmp, false);
387             @synchronized (sys->glView) {
388                 sys->place = place;
389             }
390
391             /* For resize, we call glViewport in reshape and not here.
392                This has the positive side effect that we avoid erratic sizing as we animate every resize. */
393             if (query != VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
394                 // x / y are top left corner, but we need the lower left one
395                 glViewport (place.x, cfg_tmp.display.height - (place.y + place.height), place.width, place.height);
396
397
398             [o_pool release];
399             return VLC_SUCCESS;
400         }
401
402         case VOUT_DISPLAY_HIDE_MOUSE:
403         {
404             [NSCursor setHiddenUntilMouseMoves: YES];
405             return VLC_SUCCESS;
406         }
407
408         case VOUT_DISPLAY_GET_OPENGL:
409         {
410             vlc_gl_t **gl = va_arg (ap, vlc_gl_t **);
411             *gl = &sys->gl;
412             return VLC_SUCCESS;
413         }
414
415         case VOUT_DISPLAY_RESET_PICTURES:
416             assert (0);
417         default:
418             msg_Err (vd, "Unknown request in Mac OS X vout display");
419             return VLC_EGENERIC;
420     }
421 }
422
423 /*****************************************************************************
424  * vout opengl callbacks
425  *****************************************************************************/
426 static int OpenglLock (vlc_gl_t *gl)
427 {
428     vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
429     if (!sys->glView || ![sys->glView respondsToSelector:@selector(openGLContext)])
430         return 1;
431
432     NSOpenGLContext *context = [sys->glView openGLContext];
433     CGLError err = CGLLockContext ([context CGLContextObj]);
434     if (kCGLNoError == err) {
435         [context makeCurrentContext];
436         return 0;
437     }
438     return 1;
439 }
440
441 static void OpenglUnlock (vlc_gl_t *gl)
442 {
443     vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
444     CGLUnlockContext ([[sys->glView openGLContext] CGLContextObj]);
445 }
446
447 static void OpenglSwap (vlc_gl_t *gl)
448 {
449     vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
450     [[sys->glView openGLContext] flushBuffer];
451 }
452
453 /*****************************************************************************
454  * Our NSView object
455  *****************************************************************************/
456 @implementation VLCOpenGLVideoView
457
458 #define VLCAssertMainThread() assert([[NSThread currentThread] isMainThread])
459
460
461 + (void)getNewView:(NSValue *)value
462 {
463     id *ret = [value pointerValue];
464     *ret = [[self alloc] init];
465 }
466
467 /**
468  * Gets called by the Open() method.
469  */
470 - (id)init
471 {
472     VLCAssertMainThread();
473
474     /* Warning - this may be called on non main thread */
475
476     NSOpenGLPixelFormatAttribute attribs[] =
477     {
478         NSOpenGLPFADoubleBuffer,
479         NSOpenGLPFAAccelerated,
480         NSOpenGLPFANoRecovery,
481         NSOpenGLPFAColorSize, 24,
482         NSOpenGLPFAAlphaSize, 8,
483         NSOpenGLPFADepthSize, 24,
484         NSOpenGLPFAWindow,
485         0
486     };
487
488     NSOpenGLPixelFormat *fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
489
490     if (!fmt)
491         return nil;
492
493     self = [super initWithFrame:NSMakeRect(0,0,10,10) pixelFormat:fmt];
494     [fmt release];
495
496     if (!self)
497         return nil;
498
499     /* enable HiDPI support on OS X 10.7 and later */
500     if (OSX_LION)
501         [self setWantsBestResolutionOpenGLSurface:YES];
502
503     /* Swap buffers only during the vertical retrace of the monitor.
504      http://developer.apple.com/documentation/GraphicsImaging/
505      Conceptual/OpenGL/chap5/chapter_5_section_44.html */
506     GLint params[] = { 1 };
507     CGLSetParameter ([[self openGLContext] CGLContextObj], kCGLCPSwapInterval, params);
508
509     [[NSNotificationCenter defaultCenter] addObserverForName:NSApplicationDidChangeScreenParametersNotification
510                                                       object:[NSApplication sharedApplication]
511                                                        queue:nil
512                                                   usingBlock:^(NSNotification *notification) {
513                                                       [self performSelectorOnMainThread:@selector(reshape)
514                                                                              withObject:nil
515                                                                           waitUntilDone:NO];
516                                                   }];
517
518     [self setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
519     return self;
520 }
521
522 - (void)dealloc
523 {
524     [[NSNotificationCenter defaultCenter] removeObserver:self];
525     [super dealloc];
526 }
527
528 /**
529  * Gets called by the Open() method.
530  */
531 - (void)setFrameToBoundsOfView:(NSValue *)value
532 {
533     NSView *parentView = [value pointerValue];
534     [self setFrame:[parentView bounds]];
535 }
536
537 /**
538  * Gets called by the Close and Open methods.
539  * (Non main thread).
540  */
541 - (void)setVoutDisplay:(vout_display_t *)aVd
542 {
543     @synchronized(self) {
544         vd = aVd;
545     }
546 }
547
548 /**
549  * Gets called when the vout will aquire the lock and flush.
550  * (Non main thread).
551  */
552 - (void)setVoutFlushing:(BOOL)flushing
553 {
554     if (!flushing)
555         return;
556     @synchronized(self) {
557         _hasPendingReshape = NO;
558     }
559 }
560
561 /**
562  * Can -drawRect skip rendering?.
563  */
564 - (BOOL)canSkipRendering
565 {
566     VLCAssertMainThread();
567
568     @synchronized(self) {
569         BOOL hasFirstFrame = vd && vd->sys->has_first_frame;
570         return !_hasPendingReshape && hasFirstFrame;
571     }
572 }
573
574
575 /**
576  * Local method that locks the gl context.
577  */
578 - (BOOL)lockgl
579 {
580     VLCAssertMainThread();
581     NSOpenGLContext *context = [self openGLContext];
582     CGLError err = CGLLockContext ([context CGLContextObj]);
583     if (err == kCGLNoError)
584         [context makeCurrentContext];
585     return err == kCGLNoError;
586 }
587
588 /**
589  * Local method that unlocks the gl context.
590  */
591 - (void)unlockgl
592 {
593     VLCAssertMainThread();
594     CGLUnlockContext ([[self openGLContext] CGLContextObj]);
595 }
596
597 /**
598  * Local method that force a rendering of a frame.
599  * This will get called if Cocoa forces us to redraw (via -drawRect).
600  */
601 - (void)render
602 {
603     VLCAssertMainThread();
604
605     // We may have taken some times to take the opengl Lock.
606     // Check here to see if we can just skip the frame as well.
607     if ([self canSkipRendering])
608         return;
609
610     BOOL hasFirstFrame;
611     @synchronized(self) { // vd can be accessed from multiple threads
612         hasFirstFrame = vd && vd->sys->has_first_frame;
613     }
614
615     if (hasFirstFrame)
616         // This will lock gl.
617         vout_display_opengl_Display (vd->sys->vgl, &vd->source);
618     else
619         glClear (GL_COLOR_BUFFER_BIT);
620 }
621
622 /**
623  * Method called by Cocoa when the view is resized.
624  */
625 - (void)reshape
626 {
627     VLCAssertMainThread();
628
629     NSRect bounds;
630     /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
631     if (OSX_LION)
632         bounds = [self convertRectToBacking:[self bounds]];
633     else
634         bounds = [self bounds];
635     vout_display_place_t place;
636     
637     @synchronized(self) {
638         if (vd) {
639             vout_display_cfg_t cfg_tmp = *(vd->cfg);
640             cfg_tmp.display.width  = bounds.size.width;
641             cfg_tmp.display.height = bounds.size.height;
642
643             vout_display_PlacePicture (&place, &vd->source, &cfg_tmp, false);
644             vd->sys->place = place;
645             vout_display_SendEventDisplaySize (vd, bounds.size.width, bounds.size.height, vd->cfg->is_fullscreen);
646         }
647     }
648
649     if ([self lockgl]) {
650         // x / y are top left corner, but we need the lower left one
651         glViewport (place.x, bounds.size.height - (place.y + place.height), place.width, place.height);
652
653         @synchronized(self) {
654             // This may be cleared before -drawRect is being called,
655             // in this case we'll skip the rendering.
656             // This will save us for rendering two frames (or more) for nothing
657             // (one by the vout, one (or more) by drawRect)
658             _hasPendingReshape = YES;
659         }
660
661         [self unlockgl];
662
663         [super reshape];
664     }
665 }
666
667 /**
668  * Method called by Cocoa when the view is resized or the location has changed.
669  * We just need to make sure we are locking here.
670  */
671 - (void)update
672 {
673     VLCAssertMainThread();
674     BOOL success = [self lockgl];
675     if (!success)
676         return;
677
678     [super update];
679
680     [self unlockgl];
681 }
682
683 /**
684  * Method called by Cocoa to force redraw.
685  */
686 - (void)drawRect:(NSRect) rect
687 {
688     VLCAssertMainThread();
689
690     if ([self canSkipRendering])
691         return;
692
693     BOOL success = [self lockgl];
694     if (!success)
695         return;
696
697     [self render];
698
699     [self unlockgl];
700 }
701
702 - (void)renewGState
703 {
704     NSWindow *window = [self window];
705
706     // Remove flashes with splitter view.
707     if ([window respondsToSelector:@selector(disableScreenUpdatesUntilFlush)])
708         [window disableScreenUpdatesUntilFlush];
709
710     [super renewGState];
711 }
712
713 - (BOOL)isOpaque
714 {
715     return YES;
716 }
717
718 #pragma mark -
719 #pragma mark Mouse handling
720
721 - (void)mouseDown:(NSEvent *)o_event
722 {
723     @synchronized (self) {
724         if (vd) {
725             if ([o_event type] == NSLeftMouseDown && !([o_event modifierFlags] &  NSControlKeyMask)) {
726                 if ([o_event clickCount] <= 1)
727                     vout_display_SendEventMousePressed (vd, MOUSE_BUTTON_LEFT);
728             }
729         }
730     }
731
732     [super mouseDown:o_event];
733 }
734
735 - (void)otherMouseDown:(NSEvent *)o_event
736 {
737     @synchronized (self) {
738         if (vd)
739             vout_display_SendEventMousePressed (vd, MOUSE_BUTTON_CENTER);
740     }
741
742     [super otherMouseDown: o_event];
743 }
744
745 - (void)mouseUp:(NSEvent *)o_event
746 {
747     @synchronized (self) {
748         if (vd) {
749             if ([o_event type] == NSLeftMouseUp)
750                 vout_display_SendEventMouseReleased (vd, MOUSE_BUTTON_LEFT);
751         }
752     }
753
754     [super mouseUp: o_event];
755 }
756
757 - (void)otherMouseUp:(NSEvent *)o_event
758 {
759     @synchronized (self) {
760         if (vd)
761             vout_display_SendEventMouseReleased (vd, MOUSE_BUTTON_CENTER);
762     }
763
764     [super otherMouseUp: o_event];
765 }
766
767 - (void)mouseMoved:(NSEvent *)o_event
768 {
769     NSPoint ml;
770     NSRect s_rect;
771     BOOL b_inside;
772
773     /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
774     if (OSX_LION)
775         s_rect = [self convertRectToBacking:[self bounds]];
776     else
777         s_rect = [self bounds];
778     ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
779     b_inside = [self mouse: ml inRect: s_rect];
780     
781     if (b_inside) {
782         @synchronized (self) {
783             if (vd) {
784                 vout_display_place_t place = vd->sys->place;
785
786                 if (place.width > 0 && place.height > 0) {
787                     const int x = vd->source.i_x_offset +
788                     (int64_t)(ml.x - place.x) * vd->source.i_visible_width / place.width;
789                     const int y = vd->source.i_y_offset +
790                     (int64_t)((int)s_rect.size.height - (int)ml.y - place.y) * vd->source.i_visible_height / place.height;
791
792                     vout_display_SendEventMouseMoved (vd, x, y);
793                 }
794             }
795         }
796     }
797
798     [super mouseMoved: o_event];
799 }
800
801 - (void)mouseDragged:(NSEvent *)o_event
802 {
803     [self mouseMoved: o_event];
804     [super mouseDragged: o_event];
805 }
806
807 - (void)otherMouseDragged:(NSEvent *)o_event
808 {
809     [self mouseMoved: o_event];
810     [super otherMouseDragged: o_event];
811 }
812
813 - (void)rightMouseDragged:(NSEvent *)o_event
814 {
815     [self mouseMoved: o_event];
816     [super rightMouseDragged: o_event];
817 }
818
819 - (BOOL)acceptsFirstResponder
820 {
821     return YES;
822 }
823
824 - (BOOL)mouseDownCanMoveWindow
825 {
826     return YES;
827 }
828
829 @end