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