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