vout/macosx: fix Control not working with libvlc
[vlc.git] / modules / video_output / macosx.m
blobe0c87d4126771b6eb3b050371845201835f7013a
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  *****************************************************************************/
33 /*****************************************************************************
34  * Preamble
35  *****************************************************************************/
37 #import <Cocoa/Cocoa.h>
38 #import <OpenGL/OpenGL.h>
39 #import <dlfcn.h>
41 #ifdef HAVE_CONFIG_H
42 # include "config.h"
43 #endif
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/vout_helper.h"
52 /**
53  * Forward declarations
54  */
55 static int Open (vlc_object_t *);
56 static void Close (vlc_object_t *);
58 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count);
59 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture);
60 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture);
61 static int Control (vout_display_t *vd, int query, va_list ap);
63 static void *OurGetProcAddress(vlc_gl_t *, const char *);
65 static int OpenglLock (vlc_gl_t *gl);
66 static void OpenglUnlock (vlc_gl_t *gl);
67 static void OpenglSwap (vlc_gl_t *gl);
69 /**
70  * Module declaration
71  */
72 vlc_module_begin ()
73     /* Will be loaded even without interface module. see voutgl.m */
74     set_shortname ("Mac OS X")
75     set_description (N_("Mac OS X OpenGL video output"))
76     set_category (CAT_VIDEO)
77     set_subcategory (SUBCAT_VIDEO_VOUT)
78     set_capability ("vout display", 300)
79     set_callbacks (Open, Close)
80     add_shortcut ("macosx", "vout_macosx")
81     add_glconv ()
82 vlc_module_end ()
84 /**
85  * Obj-C protocol declaration that drawable-nsobject should follow
86  */
87 @protocol VLCOpenGLVideoViewEmbedding <NSObject>
88 - (void)addVoutSubview:(NSView *)view;
89 - (void)removeVoutSubview:(NSView *)view;
90 @end
92 @interface VLCOpenGLVideoView : NSOpenGLView
94     vout_display_t *vd;
95     BOOL _hasPendingReshape;
97 - (void)setVoutDisplay:(vout_display_t *)vd;
98 - (void)setVoutFlushing:(BOOL)flushing;
99 @end
102 struct vout_display_sys_t
104     VLCOpenGLVideoView *glView;
105     id<VLCOpenGLVideoViewEmbedding> container;
107     vout_window_t *embed;
108     vlc_gl_t *gl;
109     vout_display_opengl_t *vgl;
111     picture_pool_t *pool;
112     picture_t *current;
113     bool has_first_frame;
115     vout_display_place_t place;
118 struct gl_sys
120     CGLContextObj locked_ctx;
121     VLCOpenGLVideoView *glView;
124 static void *OurGetProcAddress(vlc_gl_t *gl, const char *name)
126     VLC_UNUSED(gl);
128     return dlsym(RTLD_DEFAULT, name);
131 static int Open (vlc_object_t *this)
133     vout_display_t *vd = (vout_display_t *)this;
134     vout_display_sys_t *sys = calloc (1, sizeof(*sys));
136     if (!sys)
137         return VLC_ENOMEM;
139     @autoreleasepool {
140         if (!CGDisplayUsesOpenGLAcceleration (kCGDirectMainDisplay))
141             msg_Err (this, "no OpenGL hardware acceleration found. this can lead to slow output and unexpected results");
143         vd->sys = sys;
144         sys->pool = NULL;
145         sys->embed = NULL;
146         sys->vgl = NULL;
147         sys->gl = NULL;
149         var_Create(vd->obj.parent, "macosx-glcontext", VLC_VAR_ADDRESS);
151         /* Get the drawable object */
152         id container = var_CreateGetAddress (vd, "drawable-nsobject");
153         if (container)
154             vout_display_DeleteWindow (vd, NULL);
155         else {
156             sys->embed = vout_display_NewWindow (vd, VOUT_WINDOW_TYPE_NSOBJECT);
157             if (sys->embed)
158                 container = sys->embed->handle.nsobject;
160             if (!container) {
161                 msg_Err(vd, "No drawable-nsobject nor vout_window_t found, passing over.");
162                 goto error;
163             }
164         }
166         /* This will be released in Close(), on
167          * main thread, after we are done using it. */
168         sys->container = [container retain];
170         /* Get our main view*/
171         [VLCOpenGLVideoView performSelectorOnMainThread:@selector(getNewView:)
172                                              withObject:[NSValue valueWithPointer:&sys->glView]
173                                           waitUntilDone:YES];
174         if (!sys->glView) {
175             msg_Err(vd, "Initialization of open gl view failed");
176             goto error;
177         }
179         [sys->glView setVoutDisplay:vd];
181         /* We don't wait, that means that we'll have to be careful about releasing
182          * container.
183          * That's why we'll release on main thread in Close(). */
184         if ([(id)container respondsToSelector:@selector(addVoutSubview:)])
185             [(id)container performSelectorOnMainThread:@selector(addVoutSubview:)
186                                             withObject:sys->glView
187                                          waitUntilDone:NO];
188         else if ([container isKindOfClass:[NSView class]]) {
189             NSView *parentView = container;
190             [parentView performSelectorOnMainThread:@selector(addSubview:)
191                                          withObject:sys->glView
192                                       waitUntilDone:NO];
193             [sys->glView performSelectorOnMainThread:@selector(setFrameToBoundsOfView:)
194                                           withObject:[NSValue valueWithPointer:parentView]
195                                        waitUntilDone:NO];
196         } else {
197             msg_Err(vd, "Invalid drawable-nsobject object. drawable-nsobject must either be an NSView or comply to the @protocol VLCOpenGLVideoViewEmbedding.");
198             goto error;
199         }
201         /* Initialize common OpenGL video display */
202         sys->gl = vlc_object_create(this, sizeof(*sys->gl));
204         if( unlikely( !sys->gl ) )
205             goto error;
207         struct gl_sys *glsys = sys->gl->sys = malloc(sizeof(struct gl_sys));
208         if( unlikely( !sys->gl->sys ) )
209         {
210             vlc_object_release(sys->gl);
211             goto error;
212         }
213         glsys->locked_ctx = NULL;
214         glsys->glView = sys->glView;
215         sys->gl->makeCurrent = OpenglLock;
216         sys->gl->releaseCurrent = OpenglUnlock;
217         sys->gl->swap = OpenglSwap;
218         sys->gl->getProcAddress = OurGetProcAddress;
220         var_SetAddress(vd->obj.parent, "macosx-glcontext",
221                        [[sys->glView openGLContext] CGLContextObj]);
223         const vlc_fourcc_t *subpicture_chromas;
225         if (vlc_gl_MakeCurrent(sys->gl) != VLC_SUCCESS)
226         {
227             msg_Err(vd, "Can't attach gl context");
228             goto error;
229         }
230         sys->vgl = vout_display_opengl_New (&vd->fmt, &subpicture_chromas, sys->gl,
231                                             &vd->cfg->viewpoint);
232         vlc_gl_ReleaseCurrent(sys->gl);
233         if (!sys->vgl) {
234             msg_Err(vd, "Error while initializing opengl display.");
235             goto error;
236         }
238         /* */
239         vout_display_info_t info = vd->info;
240         info.has_pictures_invalid = false;
241         info.subpicture_chromas = subpicture_chromas;
243         /* Setup vout_display_t once everything is fine */
244         vd->info = info;
245         
246         vd->pool = Pool;
247         vd->prepare = PictureRender;
248         vd->display = PictureDisplay;
249         vd->control = Control;
250         
251         /* */
252         vout_display_SendEventDisplaySize (vd, vd->fmt.i_visible_width, vd->fmt.i_visible_height);
253         
254         return VLC_SUCCESS;
255         
256     error:
257         Close(this);
258         return VLC_EGENERIC;
259     }
262 void Close (vlc_object_t *this)
264     vout_display_t *vd = (vout_display_t *)this;
265     vout_display_sys_t *sys = vd->sys;
267     @autoreleasepool {
268         [sys->glView setVoutDisplay:nil];
270         var_Destroy (vd, "drawable-nsobject");
271         if ([(id)sys->container respondsToSelector:@selector(removeVoutSubview:)])
272         /* This will retain sys->glView */
273             [(id)sys->container performSelectorOnMainThread:@selector(removeVoutSubview:)
274                                                  withObject:sys->glView
275                                               waitUntilDone:NO];
277         /* release on main thread as explained in Open() */
278         [(id)sys->container performSelectorOnMainThread:@selector(release)
279                                              withObject:nil
280                                           waitUntilDone:NO];
281         [sys->glView performSelectorOnMainThread:@selector(removeFromSuperview)
282                                       withObject:nil
283                                    waitUntilDone:NO];
285         var_Destroy(vd->obj.parent, "macosx-glcontext");
286         if (sys->vgl != NULL)
287         {
288             vlc_gl_MakeCurrent(sys->gl);
289             vout_display_opengl_Delete (sys->vgl);
290             vlc_gl_ReleaseCurrent(sys->gl);
291         }
293         if (sys->gl != NULL)
294         {
295             assert(((struct gl_sys *)sys->gl->sys)->locked_ctx == NULL);
296             free(sys->gl->sys);
297             vlc_object_release(sys->gl);
298         }
300         [sys->glView release];
302         if (sys->embed)
303             vout_display_DeleteWindow (vd, sys->embed);
304         free (sys);
305     }
308 /*****************************************************************************
309  * vout display callbacks
310  *****************************************************************************/
312 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
314     vout_display_sys_t *sys = vd->sys;
316     if (!sys->pool && vlc_gl_MakeCurrent(sys->gl) == VLC_SUCCESS)
317     {
318         sys->pool = vout_display_opengl_GetPool (sys->vgl, requested_count);
319         vlc_gl_ReleaseCurrent(sys->gl);
320     }
321     return sys->pool;
324 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
327     vout_display_sys_t *sys = vd->sys;
329     if (vlc_gl_MakeCurrent(sys->gl) == VLC_SUCCESS)
330     {
331         vout_display_opengl_Prepare (sys->vgl, pic, subpicture);
332         vlc_gl_ReleaseCurrent(sys->gl);
333     }
336 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
338     vout_display_sys_t *sys = vd->sys;
339     [sys->glView setVoutFlushing:YES];
340     if (vlc_gl_MakeCurrent(sys->gl) == VLC_SUCCESS)
341     {
342         vout_display_opengl_Display (sys->vgl, &vd->source);
343         vlc_gl_ReleaseCurrent(sys->gl);
344     }
345     [sys->glView setVoutFlushing:NO];
346     picture_Release (pic);
347     sys->has_first_frame = true;
349     if (subpicture)
350         subpicture_Delete(subpicture);
353 static int Control (vout_display_t *vd, int query, va_list ap)
355     vout_display_sys_t *sys = vd->sys;
357     if (!vd->sys)
358         return VLC_EGENERIC;
360     @autoreleasepool {
361         switch (query)
362         {
363             case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
364             case VOUT_DISPLAY_CHANGE_ZOOM:
365             case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
366             case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
367             case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
368             {
370                 id o_window = [sys->glView window];
371                 if (!o_window) {
372                     return VLC_SUCCESS; // this is okay, since the event will occur again when we have a window
373                 }
375                 NSSize windowMinSize = [o_window minSize];
377                 const vout_display_cfg_t *cfg;
379                 if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP) {
380                     cfg = vd->cfg;
381                 } else {
382                     cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
383                 }
385                 /* we always use our current frame here, because we have some size constraints
386                  in the ui vout provider */
387                 vout_display_cfg_t cfg_tmp = *cfg;
388                 /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
389                 NSRect bounds = [sys->glView convertRectToBacking:[sys->glView bounds]];
390                 cfg_tmp.display.width = bounds.size.width;
391                 cfg_tmp.display.height = bounds.size.height;
393                 /* Reverse vertical alignment as the GL tex are Y inverted */
394                 if (cfg_tmp.align.vertical == VOUT_DISPLAY_ALIGN_TOP)
395                     cfg_tmp.align.vertical = VOUT_DISPLAY_ALIGN_BOTTOM;
396                 else if (cfg_tmp.align.vertical == VOUT_DISPLAY_ALIGN_BOTTOM)
397                     cfg_tmp.align.vertical = VOUT_DISPLAY_ALIGN_TOP;
399                 vout_display_place_t place;
400                 vout_display_PlacePicture (&place, &vd->source, &cfg_tmp, false);
401                 @synchronized (sys->glView) {
402                     sys->place = place;
403                 }
405                 if (vlc_gl_MakeCurrent (sys->gl) != VLC_SUCCESS)
406                     return VLC_EGENERIC;
407                 vout_display_opengl_SetWindowAspectRatio(sys->vgl, (float)place.width / place.height);
409                 /* For resize, we call glViewport in reshape and not here.
410                  This has the positive side effect that we avoid erratic sizing as we animate every resize. */
411                 if (query != VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
412                     // x / y are top left corner, but we need the lower left one
413                     glViewport (place.x, cfg_tmp.display.height - (place.y + place.height), place.width, place.height);
414                 vlc_gl_ReleaseCurrent (sys->gl);
416                 return VLC_SUCCESS;
417             }
419             case VOUT_DISPLAY_CHANGE_VIEWPOINT:
420                 return vout_display_opengl_SetViewpoint (sys->vgl,
421                     &va_arg (ap, const vout_display_cfg_t* )->viewpoint);
423             case VOUT_DISPLAY_RESET_PICTURES:
424                 vlc_assert_unreachable ();
425             default:
426                 msg_Err (vd, "Unknown request in Mac OS X vout display");
427                 return VLC_EGENERIC;
428         }
429     }
432 /*****************************************************************************
433  * vout opengl callbacks
434  *****************************************************************************/
435 static int OpenglLock (vlc_gl_t *gl)
437     struct gl_sys *sys = gl->sys;
438     if (![sys->glView respondsToSelector:@selector(openGLContext)])
439         return 1;
441     assert(sys->locked_ctx == NULL);
443     NSOpenGLContext *context = [sys->glView openGLContext];
444     CGLContextObj cglcntx = [context CGLContextObj];
446     CGLError err = CGLLockContext (cglcntx);
447     if (kCGLNoError == err) {
448         sys->locked_ctx = cglcntx;
449         [context makeCurrentContext];
450         return 0;
451     }
452     return 1;
455 static void OpenglUnlock (vlc_gl_t *gl)
457     struct gl_sys *sys = gl->sys;
458     CGLUnlockContext (sys->locked_ctx);
459     sys->locked_ctx = NULL;
462 static void OpenglSwap (vlc_gl_t *gl)
464     struct gl_sys *sys = gl->sys;
465     [[sys->glView openGLContext] flushBuffer];
468 /*****************************************************************************
469  * Our NSView object
470  *****************************************************************************/
471 @implementation VLCOpenGLVideoView
473 #define VLCAssertMainThread() assert([[NSThread currentThread] isMainThread])
476 + (void)getNewView:(NSValue *)value
478     id *ret = [value pointerValue];
479     *ret = [[self alloc] init];
483  * Gets called by the Open() method.
484  */
485 - (id)init
487     VLCAssertMainThread();
489     /* Warning - this may be called on non main thread */
491     NSOpenGLPixelFormatAttribute attribs[] =
492     {
493         NSOpenGLPFADoubleBuffer,
494         NSOpenGLPFAAccelerated,
495         NSOpenGLPFANoRecovery,
496         NSOpenGLPFAColorSize, 24,
497         NSOpenGLPFAAlphaSize, 8,
498         NSOpenGLPFADepthSize, 24,
499         NSOpenGLPFAWindow,
500         NSOpenGLPFAAllowOfflineRenderers,
501         0
502     };
504     NSOpenGLPixelFormat *fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
506     if (!fmt)
507         return nil;
509     self = [super initWithFrame:NSMakeRect(0,0,10,10) pixelFormat:fmt];
510     [fmt release];
512     if (!self)
513         return nil;
515     /* enable HiDPI support */
516     [self setWantsBestResolutionOpenGLSurface:YES];
518     /* request our screen's HDR mode (introduced in OS X 10.11) */
519 #pragma clang diagnostic push
520 #pragma clang diagnostic ignored "-Wpartial-availability"
521     if ([self respondsToSelector:@selector(setWantsExtendedDynamicRangeOpenGLSurface:)]) {
522         [self setWantsExtendedDynamicRangeOpenGLSurface:YES];
523     }
524 #pragma clang diagnostic pop
526     /* Swap buffers only during the vertical retrace of the monitor.
527      http://developer.apple.com/documentation/GraphicsImaging/
528      Conceptual/OpenGL/chap5/chapter_5_section_44.html */
529     GLint params[] = { 1 };
530     CGLSetParameter ([[self openGLContext] CGLContextObj], kCGLCPSwapInterval, params);
532     [[NSNotificationCenter defaultCenter] addObserverForName:NSApplicationDidChangeScreenParametersNotification
533                                                       object:[NSApplication sharedApplication]
534                                                        queue:nil
535                                                   usingBlock:^(NSNotification *notification) {
536                                                       [self performSelectorOnMainThread:@selector(reshape)
537                                                                              withObject:nil
538                                                                           waitUntilDone:NO];
539                                                   }];
541     [self setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
542     return self;
545 - (void)dealloc
547     [[NSNotificationCenter defaultCenter] removeObserver:self];
548     [super dealloc];
552  * Gets called by the Open() method.
553  */
554 - (void)setFrameToBoundsOfView:(NSValue *)value
556     NSView *parentView = [value pointerValue];
557     [self setFrame:[parentView bounds]];
561  * Gets called by the Close and Open methods.
562  * (Non main thread).
563  */
564 - (void)setVoutDisplay:(vout_display_t *)aVd
566     @synchronized(self) {
567         vd = aVd;
568     }
572  * Gets called when the vout will aquire the lock and flush.
573  * (Non main thread).
574  */
575 - (void)setVoutFlushing:(BOOL)flushing
577     if (!flushing)
578         return;
579     @synchronized(self) {
580         _hasPendingReshape = NO;
581     }
585  * Can -drawRect skip rendering?.
586  */
587 - (BOOL)canSkipRendering
589     VLCAssertMainThread();
591     @synchronized(self) {
592         BOOL hasFirstFrame = vd && vd->sys->has_first_frame;
593         return !_hasPendingReshape && hasFirstFrame;
594     }
599  * Local method that locks the gl context.
600  */
601 - (BOOL)lockgl
603     VLCAssertMainThread();
604     NSOpenGLContext *context = [self openGLContext];
605     CGLError err = CGLLockContext ([context CGLContextObj]);
606     if (err == kCGLNoError)
607         [context makeCurrentContext];
608     return err == kCGLNoError;
612  * Local method that unlocks the gl context.
613  */
614 - (void)unlockgl
616     VLCAssertMainThread();
617     CGLUnlockContext ([[self openGLContext] CGLContextObj]);
621  * Local method that force a rendering of a frame.
622  * This will get called if Cocoa forces us to redraw (via -drawRect).
623  */
624 - (void)render
626     VLCAssertMainThread();
628     // We may have taken some times to take the opengl Lock.
629     // Check here to see if we can just skip the frame as well.
630     if ([self canSkipRendering])
631         return;
633     BOOL hasFirstFrame;
634     @synchronized(self) { // vd can be accessed from multiple threads
635         hasFirstFrame = vd && vd->sys->has_first_frame;
636     }
638     if (hasFirstFrame)
639         // This will lock gl.
640         vout_display_opengl_Display (vd->sys->vgl, &vd->source);
641     else
642         glClear (GL_COLOR_BUFFER_BIT);
646  * Method called by Cocoa when the view is resized.
647  */
648 - (void)reshape
650     VLCAssertMainThread();
652     /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
653     NSRect bounds = [self convertRectToBacking:[self bounds]];
654     vout_display_place_t place;
656     @synchronized(self) {
657         if (vd) {
658             vout_display_cfg_t cfg_tmp = *(vd->cfg);
659             cfg_tmp.display.width  = bounds.size.width;
660             cfg_tmp.display.height = bounds.size.height;
662             vout_display_PlacePicture (&place, &vd->source, &cfg_tmp, false);
663             vd->sys->place = place;
664             vout_display_SendEventDisplaySize (vd, bounds.size.width, bounds.size.height);
665         }
666     }
668     if ([self lockgl]) {
669         // x / y are top left corner, but we need the lower left one
670         glViewport (place.x, bounds.size.height - (place.y + place.height), place.width, place.height);
672         @synchronized(self) {
673             // This may be cleared before -drawRect is being called,
674             // in this case we'll skip the rendering.
675             // This will save us for rendering two frames (or more) for nothing
676             // (one by the vout, one (or more) by drawRect)
677             _hasPendingReshape = YES;
678         }
680         [self unlockgl];
682         [super reshape];
683     }
687  * Method called by Cocoa when the view is resized or the location has changed.
688  * We just need to make sure we are locking here.
689  */
690 - (void)update
692     VLCAssertMainThread();
693     BOOL success = [self lockgl];
694     if (!success)
695         return;
697     [super update];
699     [self unlockgl];
703  * Method called by Cocoa to force redraw.
704  */
705 - (void)drawRect:(NSRect) rect
707     VLCAssertMainThread();
709     if ([self canSkipRendering])
710         return;
712     BOOL success = [self lockgl];
713     if (!success)
714         return;
716     [self render];
718     [self unlockgl];
721 - (void)renewGState
723     NSWindow *window = [self window];
725     // Remove flashes with splitter view.
726     if ([window respondsToSelector:@selector(disableScreenUpdatesUntilFlush)])
727         [window disableScreenUpdatesUntilFlush];
729     [super renewGState];
732 - (BOOL)isOpaque
734     return YES;
737 #pragma mark -
738 #pragma mark Mouse handling
740 - (void)mouseDown:(NSEvent *)o_event
742     @synchronized (self) {
743         if (vd) {
744             if ([o_event type] == NSLeftMouseDown && !([o_event modifierFlags] &  NSControlKeyMask)) {
745                 if ([o_event clickCount] <= 1)
746                     vout_display_SendEventMousePressed (vd, MOUSE_BUTTON_LEFT);
747             }
748         }
749     }
751     [super mouseDown:o_event];
754 - (void)otherMouseDown:(NSEvent *)o_event
756     @synchronized (self) {
757         if (vd)
758             vout_display_SendEventMousePressed (vd, MOUSE_BUTTON_CENTER);
759     }
761     [super otherMouseDown: o_event];
764 - (void)mouseUp:(NSEvent *)o_event
766     @synchronized (self) {
767         if (vd) {
768             if ([o_event type] == NSLeftMouseUp)
769                 vout_display_SendEventMouseReleased (vd, MOUSE_BUTTON_LEFT);
770         }
771     }
773     [super mouseUp: o_event];
776 - (void)otherMouseUp:(NSEvent *)o_event
778     @synchronized (self) {
779         if (vd)
780             vout_display_SendEventMouseReleased (vd, MOUSE_BUTTON_CENTER);
781     }
783     [super otherMouseUp: o_event];
786 - (void)mouseMoved:(NSEvent *)o_event
788     /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
789     NSPoint ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
790     NSRect videoRect = [self bounds];
791     BOOL b_inside = [self mouse: ml inRect: videoRect];
793     ml = [self convertPointToBacking: ml];
794     videoRect = [self convertRectToBacking: videoRect];
796     if (b_inside) {
797         @synchronized (self) {
798             if (vd) {
799                 vout_display_SendMouseMovedDisplayCoordinates(vd, ORIENT_NORMAL,
800                                                               (int)ml.x, videoRect.size.height - (int)ml.y,
801                                                               &vd->sys->place);
802             }
803         }
804     }
806     [super mouseMoved: o_event];
809 - (void)mouseDragged:(NSEvent *)o_event
811     [self mouseMoved: o_event];
812     [super mouseDragged: o_event];
815 - (void)otherMouseDragged:(NSEvent *)o_event
817     [self mouseMoved: o_event];
818     [super otherMouseDragged: o_event];
821 - (void)rightMouseDragged:(NSEvent *)o_event
823     [self mouseMoved: o_event];
824     [super rightMouseDragged: o_event];
827 - (BOOL)acceptsFirstResponder
829     return YES;
832 - (BOOL)mouseDownCanMoveWindow
834     return YES;
837 @end