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