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