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