]> git.sesse.net Git - vlc/blob - modules/video_output/macosx.m
decoder: fix data race in input_DecoderFrameNext()
[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"))
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
149     vd->sys = sys;
150     sys->pool = NULL;
151     sys->gl.sys = NULL;
152     sys->embed = NULL;
153
154     /* Get the drawable object */
155     id container = var_CreateGetAddress (vd, "drawable-nsobject");
156     if (container)
157         vout_display_DeleteWindow (vd, NULL);
158     else {
159         sys->embed = vout_display_NewWindow (vd, VOUT_WINDOW_TYPE_NSOBJECT);
160         if (sys->embed)
161             container = sys->embed->handle.nsobject;
162
163         if (!container) {
164             msg_Err(vd, "No drawable-nsobject nor vout_window_t found, passing over.");
165             goto error;
166         }
167     }
168
169     /* This will be released in Close(), on
170      * main thread, after we are done using it. */
171     sys->container = [container retain];
172
173     /* Get our main view*/
174     nsPool = [[NSAutoreleasePool alloc] init];
175
176     [VLCOpenGLVideoView performSelectorOnMainThread:@selector(getNewView:) withObject:[NSValue valueWithPointer:&sys->glView] waitUntilDone:YES];
177     if (!sys->glView) {
178         msg_Err(vd, "Initialization of open gl view failed");
179         goto error;
180     }
181
182     [sys->glView setVoutDisplay:vd];
183
184     /* We don't wait, that means that we'll have to be careful about releasing
185      * container.
186      * That's why we'll release on main thread in Close(). */
187     if ([(id)container respondsToSelector:@selector(addVoutSubview:)])
188         [(id)container performSelectorOnMainThread:@selector(addVoutSubview:) withObject:sys->glView waitUntilDone:NO];
189     else if ([container isKindOfClass:[NSView class]]) {
190         NSView *parentView = container;
191         [parentView performSelectorOnMainThread:@selector(addSubview:) withObject:sys->glView waitUntilDone:NO];
192         [sys->glView performSelectorOnMainThread:@selector(setFrameToBoundsOfView:) withObject:[NSValue valueWithPointer:parentView] waitUntilDone:NO];
193     } else {
194         msg_Err(vd, "Invalid drawable-nsobject object. drawable-nsobject must either be an NSView or comply to the @protocol VLCOpenGLVideoViewEmbedding.");
195         goto error;
196     }
197
198
199     [nsPool release];
200     nsPool = nil;
201
202     /* Initialize common OpenGL video display */
203     sys->gl.lock = OpenglLock;
204     sys->gl.unlock = OpenglUnlock;
205     sys->gl.swap = OpenglSwap;
206     sys->gl.getProcAddress = OurGetProcAddress;
207     sys->gl.sys = sys;
208     const vlc_fourcc_t *subpicture_chromas;
209
210     sys->vgl = vout_display_opengl_New (&vd->fmt, &subpicture_chromas, &sys->gl);
211     if (!sys->vgl) {
212         msg_Err(vd, "Error while initializing opengl display.");
213         sys->gl.sys = NULL;
214         goto error;
215     }
216
217     /* */
218     vout_display_info_t info = vd->info;
219     info.has_pictures_invalid = false;
220     info.has_event_thread = true;
221     info.subpicture_chromas = subpicture_chromas;
222     info.has_hide_mouse = true;
223
224     /* Setup vout_display_t once everything is fine */
225     vd->info = info;
226
227     vd->pool = Pool;
228     vd->prepare = PictureRender;
229     vd->display = PictureDisplay;
230     vd->control = Control;
231
232     /* */
233     vout_display_SendEventDisplaySize (vd, vd->fmt.i_visible_width, vd->fmt.i_visible_height);
234
235     return VLC_SUCCESS;
236
237 error:
238     [nsPool release];
239     Close(this);
240     return VLC_EGENERIC;
241 }
242
243 void Close (vlc_object_t *this)
244 {
245     vout_display_t *vd = (vout_display_t *)this;
246     vout_display_sys_t *sys = vd->sys;
247
248     [sys->glView setVoutDisplay:nil];
249
250     var_Destroy (vd, "drawable-nsobject");
251     if ([(id)sys->container respondsToSelector:@selector(removeVoutSubview:)])
252         /* This will retain sys->glView */
253         [(id)sys->container performSelectorOnMainThread:@selector(removeVoutSubview:) withObject:sys->glView waitUntilDone:NO];
254
255     /* release on main thread as explained in Open() */
256     [(id)sys->container performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
257     [sys->glView performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
258
259     if (sys->gl.sys != NULL)
260         vout_display_opengl_Delete (sys->vgl);
261
262     [sys->glView release];
263
264     if (sys->embed)
265         vout_display_DeleteWindow (vd, sys->embed);
266     free (sys);
267 }
268
269 /*****************************************************************************
270  * vout display callbacks
271  *****************************************************************************/
272
273 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
274 {
275     vout_display_sys_t *sys = vd->sys;
276
277     if (!sys->pool)
278         sys->pool = vout_display_opengl_GetPool (sys->vgl, requested_count);
279     assert(sys->pool);
280     return sys->pool;
281 }
282
283 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
284 {
285
286     vout_display_sys_t *sys = vd->sys;
287
288     vout_display_opengl_Prepare (sys->vgl, pic, subpicture);
289 }
290
291 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
292 {
293     vout_display_sys_t *sys = vd->sys;
294     [sys->glView setVoutFlushing:YES];
295     vout_display_opengl_Display (sys->vgl, &vd->source);
296     [sys->glView setVoutFlushing:NO];
297     picture_Release (pic);
298     sys->has_first_frame = true;
299
300     if (subpicture)
301         subpicture_Delete(subpicture);
302 }
303
304 static int Control (vout_display_t *vd, int query, va_list ap)
305 {
306     vout_display_sys_t *sys = vd->sys;
307
308     if (!vd->sys)
309         return VLC_EGENERIC;
310
311     if (!sys->embed)
312         return VLC_EGENERIC;
313
314     switch (query)
315     {
316         case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
317         case VOUT_DISPLAY_CHANGE_ZOOM:
318         case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
319         case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
320         case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
321         {
322             NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
323
324             id o_window = [sys->glView window];
325             if (!o_window) {
326                 [o_pool release];
327                 return VLC_SUCCESS; // this is okay, since the event will occur again when we have a window
328             }
329
330             NSSize windowMinSize = [o_window minSize];
331
332             const vout_display_cfg_t *cfg;
333             const video_format_t *source;
334
335             if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP) {
336                 source = (const video_format_t *)va_arg (ap, const video_format_t *);
337                 cfg = vd->cfg;
338             } else {
339                 source = &vd->source;
340                 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
341             }
342
343             /* we always use our current frame here, because we have some size constraints
344                in the ui vout provider */
345             vout_display_cfg_t cfg_tmp = *cfg;
346             NSRect bounds;
347             /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
348             if (OSX_LION)
349                 bounds = [sys->glView convertRectToBacking:[sys->glView bounds]];
350             else
351                 bounds = [sys->glView bounds];
352             cfg_tmp.display.width = bounds.size.width;
353             cfg_tmp.display.height = bounds.size.height;
354
355             vout_display_place_t place;
356             vout_display_PlacePicture (&place, source, &cfg_tmp, false);
357             @synchronized (sys->glView) {
358                 sys->place = place;
359             }
360
361             /* For resize, we call glViewport in reshape and not here.
362                This has the positive side effect that we avoid erratic sizing as we animate every resize. */
363             if (query != VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
364                 // x / y are top left corner, but we need the lower left one
365                 glViewport (place.x, cfg_tmp.display.height - (place.y + place.height), place.width, place.height);
366
367
368             [o_pool release];
369             return VLC_SUCCESS;
370         }
371
372         case VOUT_DISPLAY_HIDE_MOUSE:
373         {
374             [NSCursor setHiddenUntilMouseMoves: YES];
375             return VLC_SUCCESS;
376         }
377
378         case VOUT_DISPLAY_RESET_PICTURES:
379             vlc_assert_unreachable ();
380         default:
381             msg_Err (vd, "Unknown request in Mac OS X vout display");
382             return VLC_EGENERIC;
383     }
384 }
385
386 /*****************************************************************************
387  * vout opengl callbacks
388  *****************************************************************************/
389 static int OpenglLock (vlc_gl_t *gl)
390 {
391     vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
392     if (!sys->glView || ![sys->glView respondsToSelector:@selector(openGLContext)])
393         return 1;
394
395     NSOpenGLContext *context = [sys->glView openGLContext];
396     CGLError err = CGLLockContext ([context CGLContextObj]);
397     if (kCGLNoError == err) {
398         [context makeCurrentContext];
399         return 0;
400     }
401     return 1;
402 }
403
404 static void OpenglUnlock (vlc_gl_t *gl)
405 {
406     vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
407     CGLUnlockContext ([[sys->glView openGLContext] CGLContextObj]);
408 }
409
410 static void OpenglSwap (vlc_gl_t *gl)
411 {
412     vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
413     [[sys->glView openGLContext] flushBuffer];
414 }
415
416 /*****************************************************************************
417  * Our NSView object
418  *****************************************************************************/
419 @implementation VLCOpenGLVideoView
420
421 #define VLCAssertMainThread() assert([[NSThread currentThread] isMainThread])
422
423
424 + (void)getNewView:(NSValue *)value
425 {
426     id *ret = [value pointerValue];
427     *ret = [[self alloc] init];
428 }
429
430 /**
431  * Gets called by the Open() method.
432  */
433 - (id)init
434 {
435     VLCAssertMainThread();
436
437     /* Warning - this may be called on non main thread */
438
439     NSOpenGLPixelFormatAttribute attribs[] =
440     {
441         NSOpenGLPFADoubleBuffer,
442         NSOpenGLPFAAccelerated,
443         NSOpenGLPFANoRecovery,
444         NSOpenGLPFAColorSize, 24,
445         NSOpenGLPFAAlphaSize, 8,
446         NSOpenGLPFADepthSize, 24,
447         NSOpenGLPFAWindow,
448         NSOpenGLPFAAllowOfflineRenderers,
449         0
450     };
451
452     NSOpenGLPixelFormat *fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
453
454     if (!fmt)
455         return nil;
456
457     self = [super initWithFrame:NSMakeRect(0,0,10,10) pixelFormat:fmt];
458     [fmt release];
459
460     if (!self)
461         return nil;
462
463     /* enable HiDPI support on OS X 10.7 and later */
464     if (OSX_LION)
465         [self setWantsBestResolutionOpenGLSurface:YES];
466
467     /* Swap buffers only during the vertical retrace of the monitor.
468      http://developer.apple.com/documentation/GraphicsImaging/
469      Conceptual/OpenGL/chap5/chapter_5_section_44.html */
470     GLint params[] = { 1 };
471     CGLSetParameter ([[self openGLContext] CGLContextObj], kCGLCPSwapInterval, params);
472
473     [[NSNotificationCenter defaultCenter] addObserverForName:NSApplicationDidChangeScreenParametersNotification
474                                                       object:[NSApplication sharedApplication]
475                                                        queue:nil
476                                                   usingBlock:^(NSNotification *notification) {
477                                                       [self performSelectorOnMainThread:@selector(reshape)
478                                                                              withObject:nil
479                                                                           waitUntilDone:NO];
480                                                   }];
481
482     [self setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
483     return self;
484 }
485
486 - (void)dealloc
487 {
488     [[NSNotificationCenter defaultCenter] removeObserver:self];
489     [super dealloc];
490 }
491
492 /**
493  * Gets called by the Open() method.
494  */
495 - (void)setFrameToBoundsOfView:(NSValue *)value
496 {
497     NSView *parentView = [value pointerValue];
498     [self setFrame:[parentView bounds]];
499 }
500
501 /**
502  * Gets called by the Close and Open methods.
503  * (Non main thread).
504  */
505 - (void)setVoutDisplay:(vout_display_t *)aVd
506 {
507     @synchronized(self) {
508         vd = aVd;
509     }
510 }
511
512 /**
513  * Gets called when the vout will aquire the lock and flush.
514  * (Non main thread).
515  */
516 - (void)setVoutFlushing:(BOOL)flushing
517 {
518     if (!flushing)
519         return;
520     @synchronized(self) {
521         _hasPendingReshape = NO;
522     }
523 }
524
525 /**
526  * Can -drawRect skip rendering?.
527  */
528 - (BOOL)canSkipRendering
529 {
530     VLCAssertMainThread();
531
532     @synchronized(self) {
533         BOOL hasFirstFrame = vd && vd->sys->has_first_frame;
534         return !_hasPendingReshape && hasFirstFrame;
535     }
536 }
537
538
539 /**
540  * Local method that locks the gl context.
541  */
542 - (BOOL)lockgl
543 {
544     VLCAssertMainThread();
545     NSOpenGLContext *context = [self openGLContext];
546     CGLError err = CGLLockContext ([context CGLContextObj]);
547     if (err == kCGLNoError)
548         [context makeCurrentContext];
549     return err == kCGLNoError;
550 }
551
552 /**
553  * Local method that unlocks the gl context.
554  */
555 - (void)unlockgl
556 {
557     VLCAssertMainThread();
558     CGLUnlockContext ([[self openGLContext] CGLContextObj]);
559 }
560
561 /**
562  * Local method that force a rendering of a frame.
563  * This will get called if Cocoa forces us to redraw (via -drawRect).
564  */
565 - (void)render
566 {
567     VLCAssertMainThread();
568
569     // We may have taken some times to take the opengl Lock.
570     // Check here to see if we can just skip the frame as well.
571     if ([self canSkipRendering])
572         return;
573
574     BOOL hasFirstFrame;
575     @synchronized(self) { // vd can be accessed from multiple threads
576         hasFirstFrame = vd && vd->sys->has_first_frame;
577     }
578
579     if (hasFirstFrame)
580         // This will lock gl.
581         vout_display_opengl_Display (vd->sys->vgl, &vd->source);
582     else
583         glClear (GL_COLOR_BUFFER_BIT);
584 }
585
586 /**
587  * Method called by Cocoa when the view is resized.
588  */
589 - (void)reshape
590 {
591     VLCAssertMainThread();
592
593     NSRect bounds;
594     /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
595     if (OSX_LION)
596         bounds = [self convertRectToBacking:[self bounds]];
597     else
598         bounds = [self bounds];
599     vout_display_place_t place;
600
601     @synchronized(self) {
602         if (vd) {
603             vout_display_cfg_t cfg_tmp = *(vd->cfg);
604             cfg_tmp.display.width  = bounds.size.width;
605             cfg_tmp.display.height = bounds.size.height;
606
607             vout_display_PlacePicture (&place, &vd->source, &cfg_tmp, false);
608             vd->sys->place = place;
609             vout_display_SendEventDisplaySize (vd, bounds.size.width, bounds.size.height);
610         }
611     }
612
613     if ([self lockgl]) {
614         // x / y are top left corner, but we need the lower left one
615         glViewport (place.x, bounds.size.height - (place.y + place.height), place.width, place.height);
616
617         @synchronized(self) {
618             // This may be cleared before -drawRect is being called,
619             // in this case we'll skip the rendering.
620             // This will save us for rendering two frames (or more) for nothing
621             // (one by the vout, one (or more) by drawRect)
622             _hasPendingReshape = YES;
623         }
624
625         [self unlockgl];
626
627         [super reshape];
628     }
629 }
630
631 /**
632  * Method called by Cocoa when the view is resized or the location has changed.
633  * We just need to make sure we are locking here.
634  */
635 - (void)update
636 {
637     VLCAssertMainThread();
638     BOOL success = [self lockgl];
639     if (!success)
640         return;
641
642     [super update];
643
644     [self unlockgl];
645 }
646
647 /**
648  * Method called by Cocoa to force redraw.
649  */
650 - (void)drawRect:(NSRect) rect
651 {
652     VLCAssertMainThread();
653
654     if ([self canSkipRendering])
655         return;
656
657     BOOL success = [self lockgl];
658     if (!success)
659         return;
660
661     [self render];
662
663     [self unlockgl];
664 }
665
666 - (void)renewGState
667 {
668     NSWindow *window = [self window];
669
670     // Remove flashes with splitter view.
671     if ([window respondsToSelector:@selector(disableScreenUpdatesUntilFlush)])
672         [window disableScreenUpdatesUntilFlush];
673
674     [super renewGState];
675 }
676
677 - (BOOL)isOpaque
678 {
679     return YES;
680 }
681
682 #pragma mark -
683 #pragma mark Mouse handling
684
685 - (void)mouseDown:(NSEvent *)o_event
686 {
687     @synchronized (self) {
688         if (vd) {
689             if ([o_event type] == NSLeftMouseDown && !([o_event modifierFlags] &  NSControlKeyMask)) {
690                 if ([o_event clickCount] <= 1)
691                     vout_display_SendEventMousePressed (vd, MOUSE_BUTTON_LEFT);
692             }
693         }
694     }
695
696     [super mouseDown:o_event];
697 }
698
699 - (void)otherMouseDown:(NSEvent *)o_event
700 {
701     @synchronized (self) {
702         if (vd)
703             vout_display_SendEventMousePressed (vd, MOUSE_BUTTON_CENTER);
704     }
705
706     [super otherMouseDown: o_event];
707 }
708
709 - (void)mouseUp:(NSEvent *)o_event
710 {
711     @synchronized (self) {
712         if (vd) {
713             if ([o_event type] == NSLeftMouseUp)
714                 vout_display_SendEventMouseReleased (vd, MOUSE_BUTTON_LEFT);
715         }
716     }
717
718     [super mouseUp: o_event];
719 }
720
721 - (void)otherMouseUp:(NSEvent *)o_event
722 {
723     @synchronized (self) {
724         if (vd)
725             vout_display_SendEventMouseReleased (vd, MOUSE_BUTTON_CENTER);
726     }
727
728     [super otherMouseUp: o_event];
729 }
730
731 - (void)mouseMoved:(NSEvent *)o_event
732 {
733     /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
734     NSPoint ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
735     NSRect videoRect = [self bounds];
736     BOOL b_inside = [self mouse: ml inRect: videoRect];
737
738     if (OSX_LION) {
739         ml = [self convertPointToBacking: ml];
740         videoRect = [self convertRectToBacking: videoRect];
741     }
742
743     if (b_inside) {
744         @synchronized (self) {
745             if (vd) {
746                 vout_display_SendMouseMovedDisplayCoordinates(vd, ORIENT_NORMAL,
747                                                               (int)ml.x, videoRect.size.height - (int)ml.y,
748                                                               &vd->sys->place);
749             }
750         }
751     }
752
753     [super mouseMoved: o_event];
754 }
755
756 - (void)mouseDragged:(NSEvent *)o_event
757 {
758     [self mouseMoved: o_event];
759     [super mouseDragged: o_event];
760 }
761
762 - (void)otherMouseDragged:(NSEvent *)o_event
763 {
764     [self mouseMoved: o_event];
765     [super otherMouseDragged: o_event];
766 }
767
768 - (void)rightMouseDragged:(NSEvent *)o_event
769 {
770     [self mouseMoved: o_event];
771     [super rightMouseDragged: o_event];
772 }
773
774 - (BOOL)acceptsFirstResponder
775 {
776     return YES;
777 }
778
779 - (BOOL)mouseDownCanMoveWindow
780 {
781     return YES;
782 }
783
784 @end