diff options
Diffstat (limited to 'video/out/cocoa_common.m')
-rw-r--r-- | video/out/cocoa_common.m | 1047 |
1 files changed, 509 insertions, 538 deletions
diff --git a/video/out/cocoa_common.m b/video/out/cocoa_common.m index 8e06e637a4..dc95e537a1 100644 --- a/video/out/cocoa_common.m +++ b/video/out/cocoa_common.m @@ -31,145 +31,89 @@ #include "vo.h" #include "aspect.h" -#include "core/mp_fifo.h" #include "talloc.h" -#include "core/input/input.h" -#include "core/input/keycodes.h" -#include "osx_common.h" #include "core/mp_msg.h" -#ifndef NSOpenGLPFAOpenGLProfile -#define NSOpenGLPFAOpenGLProfile 99 -#endif +#include "osdep/macosx_application.h" +#include "osdep/macosx_events.h" +#include "osdep/macosx_compat.h" -#ifndef NSOpenGLProfileVersionLegacy -#define NSOpenGLProfileVersionLegacy 0x1000 -#endif - -#ifndef NSOpenGLProfileVersion3_2Core -#define NSOpenGLProfileVersion3_2Core 0x3200 -#endif - -#define NSLeftAlternateKeyMask (0x000020 | NSAlternateKeyMask) -#define NSRightAlternateKeyMask (0x000040 | NSAlternateKeyMask) - -static bool LeftAltPressed(NSEvent *event) -{ - return ([event modifierFlags] & NSLeftAlternateKeyMask) == - NSLeftAlternateKeyMask; -} - -static bool RightAltPressed(NSEvent *event) -{ - return ([event modifierFlags] & NSRightAlternateKeyMask) == - NSRightAlternateKeyMask; -} - -// add methods not available on OSX versions prior to 10.7 -#ifndef MAC_OS_X_VERSION_10_7 -@interface NSView (IntroducedInLion) -- (NSRect)convertRectToBacking:(NSRect)aRect; -- (void)setWantsBestResolutionOpenGLSurface:(BOOL)aBool; -@end -@interface NSEvent (IntroducedInLion) -- (BOOL)hasPreciseScrollingDeltas; -@end -#endif - -// add power management assertion not available on OSX versions prior to 10.7 -#ifndef kIOPMAssertionTypePreventUserIdleDisplaySleep -#define kIOPMAssertionTypePreventUserIdleDisplaySleep \ - CFSTR("PreventUserIdleDisplaySleep") -#endif - -@interface GLMPlayerWindow : NSWindow <NSWindowDelegate> { - struct vo *_vo; -} -- (void)setVideoOutput:(struct vo *)vo; +@interface GLMPlayerWindow : NSWindow <NSWindowDelegate> - (BOOL)canBecomeKeyWindow; - (BOOL)canBecomeMainWindow; - (void)fullscreen; -- (void)mouseEvent:(NSEvent *)theEvent; - (void)mulSize:(float)multiplier; - (int)titleHeight; - (NSRect)clipFrame:(NSRect)frame withContentAspect:(NSSize) aspect; - (void)setContentSize:(NSSize)newSize keepCentered:(BOOL)keepCentered; +@property(nonatomic, assign) struct vo *videoOutput; @end @interface GLMPlayerOpenGLView : NSView +@property(nonatomic, assign) struct vo *videoOutput; +- (BOOL)containsCurrentMouseLocation; +- (void)mouseEvent:(NSEvent *)theEvent; +@property(nonatomic, assign, getter=hasMouseDown) BOOL mouseDown; +@end + +@interface NSScreen (mpvadditions) +- (BOOL)hasDock; +- (BOOL)hasMenubar; @end struct vo_cocoa_state { - NSAutoreleasePool *pool; GLMPlayerWindow *window; + GLMPlayerOpenGLView *view; NSOpenGLContext *glContext; NSOpenGLPixelFormat *pixelFormat; NSSize current_video_size; NSSize previous_video_size; - NSRect screen_frame; - NSRect fsscreen_frame; - NSScreen *screen_handle; - - NSInteger windowed_mask; - NSInteger fullscreen_mask; - - NSRect windowed_frame; - - NSString *window_title; + NSScreen *current_screen; + NSScreen *fs_screen; NSInteger window_level; - int display_cursor; - int cursor_timer; - int vo_cursor_autohide_delay; + struct aspect_data aspdat; bool did_resize; + bool did_async_resize; bool out_fs_resize; + bool want_redraw; IOPMAssertionID power_mgmt_assertion; CGFloat accumulated_scroll; -}; -static int _instances = 0; - -static void create_menu(void); + NSLock *lock; + bool enable_resize_redraw; + void (*resize_redraw)(struct vo *vo, int w, int h); +}; static struct vo_cocoa_state *vo_cocoa_init_state(struct vo *vo) { struct vo_cocoa_state *s = talloc_ptrtype(vo, s); *s = (struct vo_cocoa_state){ - .pool = [[NSAutoreleasePool alloc] init], .did_resize = NO, + .did_async_resize = NO, + .want_redraw = NO, .current_video_size = {0,0}, .previous_video_size = {0,0}, - .windowed_mask = NSTitledWindowMask|NSClosableWindowMask| - NSMiniaturizableWindowMask|NSResizableWindowMask, - .fullscreen_mask = NSBorderlessWindowMask, - .windowed_frame = {{0,0},{0,0}}, .out_fs_resize = NO, - .display_cursor = 1, - .vo_cursor_autohide_delay = vo->opts->cursor_autohide_delay, .power_mgmt_assertion = kIOPMNullAssertionID, .accumulated_scroll = 0, + .lock = [[NSLock alloc] init], + .enable_resize_redraw = NO, }; - if (!vo->opts->border) s->windowed_mask = NSBorderlessWindowMask; return s; } -static bool supports_hidpi(NSView *view) -{ - SEL hdpi_selector = @selector(setWantsBestResolutionOpenGLSurface:); - return is_osx_version_at_least(10, 7, 0) && view && - [view respondsToSelector:hdpi_selector]; -} - -bool vo_cocoa_gui_running(void) +static NSRect to_pixels(struct vo *vo, NSRect frame) { - return _instances > 0; + struct vo_cocoa_state *s = vo->cocoa; + return [s->view convertRectToBacking: frame]; } void *vo_cocoa_glgetaddr(const char *s) @@ -197,62 +141,59 @@ static void disable_power_management(struct vo *vo) { struct vo_cocoa_state *s = vo->cocoa; if (s->power_mgmt_assertion) return; - - CFStringRef assertion_type = kIOPMAssertionTypeNoDisplaySleep; - if (is_osx_version_at_least(10, 7, 0)) - assertion_type = kIOPMAssertionTypePreventUserIdleDisplaySleep; - - IOPMAssertionCreateWithName(assertion_type, kIOPMAssertionLevelOn, - CFSTR("io.mpv.power_management"), &s->power_mgmt_assertion); + IOPMAssertionCreateWithName( + kIOPMAssertionTypePreventUserIdleDisplaySleep, + kIOPMAssertionLevelOn, + CFSTR("io.mpv.video_playing_back"), + &s->power_mgmt_assertion); } int vo_cocoa_init(struct vo *vo) { vo->cocoa = vo_cocoa_init_state(vo); - vo->wakeup_period = 0.02; - _instances++; - - NSApplicationLoad(); - NSApp = [NSApplication sharedApplication]; - [NSApp setActivationPolicy: NSApplicationActivationPolicyRegular]; - disable_power_management(vo); return 1; } -void vo_cocoa_uninit(struct vo *vo) +static void vo_cocoa_set_cursor_visibility(struct vo *vo, bool visible) { struct vo_cocoa_state *s = vo->cocoa; - CGDisplayShowCursor(kCGDirectMainDisplay); - enable_power_management(vo); - [NSApp setPresentationOptions:NSApplicationPresentationDefault]; - - [s->window release]; - s->window = nil; - [s->glContext release]; - s->glContext = nil; - [s->pool release]; - s->pool = nil; - _instances--; + if (visible) { + // show cursor unconditionally + CGDisplayShowCursor(kCGDirectMainDisplay); + } else if (vo->opts->fs && + [s->view containsCurrentMouseLocation] && + ![s->view hasMouseDown]) { + // only hide cursor if in fullscreen and the video view contains the + // mouse location + CGDisplayHideCursor(kCGDirectMainDisplay); + } } -void vo_cocoa_pause(struct vo *vo) +void vo_cocoa_uninit(struct vo *vo) { - enable_power_management(vo); -} + dispatch_sync(dispatch_get_main_queue(), ^{ + struct vo_cocoa_state *s = vo->cocoa; + vo_cocoa_set_cursor_visibility(vo, true); + enable_power_management(vo); + [NSApp setPresentationOptions:NSApplicationPresentationDefault]; -void vo_cocoa_resume(struct vo *vo) -{ - disable_power_management(vo); + if (vo->opts->fs) + [[s->view window] release]; + + [s->window release]; + s->window = nil; + [s->glContext release]; + s->glContext = nil; + }); } -static int current_screen_has_dock_or_menubar(struct vo *vo) +void vo_cocoa_register_resize_callback(struct vo *vo, + void (*cb)(struct vo *vo, int w, int h)) { struct vo_cocoa_state *s = vo->cocoa; - NSRect f = s->screen_frame; - NSRect vf = [s->screen_handle visibleFrame]; - return f.size.height > vf.size.height || f.size.width > vf.size.width; + s->resize_redraw = cb; } static int get_screen_handle(int identifier, NSWindow *window, NSScreen **screen) { @@ -281,46 +222,30 @@ static void update_screen_info(struct vo *vo) { struct vo_cocoa_state *s = vo->cocoa; struct mp_vo_opts *opts = vo->opts; - NSScreen *ws, *fss; - - get_screen_handle(opts->screen_id, s->window, &ws); - s->screen_frame = [ws frame]; - - get_screen_handle(opts->fsscreen_id, s->window, &fss); - s->fsscreen_frame = [fss frame]; + get_screen_handle(opts->screen_id, s->window, &s->current_screen); + get_screen_handle(opts->fsscreen_id, s->window, &s->fs_screen); } -void vo_cocoa_update_xinerama_info(struct vo *vo) +static void vo_cocoa_update_screen_info(struct vo *vo) { struct vo_cocoa_state *s = vo->cocoa; struct mp_vo_opts *opts = vo->opts; update_screen_info(vo); - aspect_save_screenres(vo, s->screen_frame.size.width, - s->screen_frame.size.height); - opts->screenwidth = s->screen_frame.size.width; - opts->screenheight = s->screen_frame.size.height; - vo->xinerama_x = s->screen_frame.origin.x; - vo->xinerama_y = s->screen_frame.origin.y; -} -int vo_cocoa_change_attributes(struct vo *vo) -{ - return 0; + NSRect r = [s->current_screen frame]; + + aspect_save_screenres(vo, r.size.width, r.size.height); + opts->screenwidth = r.size.width; + opts->screenheight = r.size.height; + vo->xinerama_x = r.origin.x; + vo->xinerama_y = r.origin.y; } static void resize_window(struct vo *vo) { struct vo_cocoa_state *s = vo->cocoa; - NSView *view = [s->window contentView]; - NSRect frame; - - if (supports_hidpi(view)) { - frame = [view convertRectToBacking: [view frame]]; - } else { - frame = [view frame]; - } - + NSRect frame = to_pixels(vo, [s->view frame]); vo->dwidth = frame.size.width; vo->dheight = frame.size.height; [s->glContext update]; @@ -335,7 +260,8 @@ static void vo_set_level(struct vo *vo, int ontop) s->window_level = NSNormalWindowLevel; } - [s->window setLevel:s->window_level]; + [[s->view window] setLevel:s->window_level]; + [s->window setLevel:s->window_level]; } void vo_cocoa_ontop(struct vo *vo) @@ -353,87 +279,145 @@ static void update_state_sizes(struct vo_cocoa_state *s, s->current_video_size = NSMakeSize(d_width, d_height); } -static int create_window(struct vo *vo, uint32_t d_width, uint32_t d_height, - uint32_t flags, int gl3profile) +static void resize_window_from_stored_size(struct vo *vo) +{ + struct vo_cocoa_state *s = vo->cocoa; + [s->window setContentSize:s->current_video_size keepCentered:YES]; + [s->window setContentAspectRatio:s->current_video_size]; +} + +static void create_window(struct vo *vo, uint32_t d_width, uint32_t d_height, + uint32_t flags) { struct vo_cocoa_state *s = vo->cocoa; - const NSRect window_rect = NSMakeRect(vo->xinerama_x, vo->xinerama_y, - d_width, d_height); - const NSRect glview_rect = NSMakeRect(0, 0, 100, 100); + struct mp_vo_opts *opts = vo->opts; + + const NSRect contentRect = NSMakeRect(0, 0, d_width, d_height); + + int window_mask = 0; + if (opts->border) { + window_mask = NSTitledWindowMask|NSClosableWindowMask| + NSMiniaturizableWindowMask|NSResizableWindowMask; + } else { + window_mask = NSBorderlessWindowMask; + } s->window = - [[GLMPlayerWindow alloc] initWithContentRect:window_rect - styleMask:s->windowed_mask + [[GLMPlayerWindow alloc] initWithContentRect:contentRect + styleMask:window_mask backing:NSBackingStoreBuffered defer:NO]; - GLMPlayerOpenGLView *glView = - [[GLMPlayerOpenGLView alloc] initWithFrame:glview_rect]; + s->view = [[GLMPlayerOpenGLView alloc] initWithFrame:contentRect]; + [s->view setWantsBestResolutionOpenGLSurface:YES]; + + cocoa_register_menu_item_action(MPM_H_SIZE, @selector(halfSize)); + cocoa_register_menu_item_action(MPM_N_SIZE, @selector(normalSize)); + cocoa_register_menu_item_action(MPM_D_SIZE, @selector(doubleSize)); + cocoa_register_menu_item_action(MPM_MINIMIZE, @selector(performMiniaturize:)); + cocoa_register_menu_item_action(MPM_ZOOM, @selector(performZoom:)); + + [s->window setRestorable:NO]; + [s->window setContentView:s->view]; + [s->view release]; + [s->window setAcceptsMouseMovedEvents:YES]; + [s->glContext setView:s->view]; + s->window.videoOutput = vo; + s->view.videoOutput = vo; + + [s->window setDelegate:s->window]; + [s->window makeMainWindow]; + + [s->window setFrameOrigin:NSMakePoint(vo->dx, vo->dy)]; + [s->window makeKeyAndOrderFront:nil]; + [NSApp activateIgnoringOtherApps:YES]; + + if (flags & VOFLAG_FULLSCREEN) + vo_cocoa_fullscreen(vo); - // check for HiDPI support and enable it (available on 10.7 +) - if (supports_hidpi(glView)) - [glView setWantsBestResolutionOpenGLSurface:YES]; + vo_set_level(vo, opts->ontop); - int i = 0; - NSOpenGLPixelFormatAttribute attr[32]; - if (is_osx_version_at_least(10, 7, 0)) { - attr[i++] = NSOpenGLPFAOpenGLProfile; - if (gl3profile) { - attr[i++] = NSOpenGLProfileVersion3_2Core; - } else { - attr[i++] = NSOpenGLProfileVersionLegacy; - } + if (opts->native_fs) { + [s->window setCollectionBehavior: + NSWindowCollectionBehaviorFullScreenPrimary]; + [NSApp setPresentationOptions:NSFullScreenWindowMask]; } - attr[i++] = NSOpenGLPFADoubleBuffer; // double buffered - attr[i] = (NSOpenGLPixelFormatAttribute)0; +} + +static NSOpenGLPixelFormatAttribute get_nsopengl_profile(int gl3profile) { + if (gl3profile) { + return NSOpenGLProfileVersion3_2Core; + } else { + return NSOpenGLProfileVersionLegacy; + } +} + +static int create_gl_context(struct vo *vo, int gl3profile) +{ + struct vo_cocoa_state *s = vo->cocoa; + + NSOpenGLPixelFormatAttribute attr[] = { + NSOpenGLPFAOpenGLProfile, + get_nsopengl_profile(gl3profile), + NSOpenGLPFADoubleBuffer, + 0 + }; s->pixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attr] autorelease]; + if (!s->pixelFormat) { mp_msg(MSGT_VO, MSGL_ERR, - "[cocoa] Invalid pixel format attribute " - "(GL3 not supported?)\n"); + "[cocoa] Trying to build invalid OpenGL pixel format\n"); return -1; } + s->glContext = [[NSOpenGLContext alloc] initWithFormat:s->pixelFormat shareContext:nil]; - create_menu(); - - [s->window setContentView:glView]; - [glView release]; - [s->window setAcceptsMouseMovedEvents:YES]; - [s->glContext setView:glView]; [s->glContext makeCurrentContext]; - [s->window setVideoOutput:vo]; - - [NSApp setDelegate:s->window]; - [s->window setDelegate:s->window]; - - [s->window setContentSize:s->current_video_size keepCentered:YES]; - [s->window setContentAspectRatio:s->current_video_size]; return 0; } +static void cocoa_set_window_title(struct vo *vo, const char *title) +{ + struct vo_cocoa_state *s = vo->cocoa; + [s->window setTitle: [NSString stringWithUTF8String:title]]; +} + static void update_window(struct vo *vo) { struct vo_cocoa_state *s = vo->cocoa; - if (s->current_video_size.width != s->previous_video_size.width || - s->current_video_size.height != s->previous_video_size.height) { + if (!CGSizeEqualToSize(s->current_video_size, s->previous_video_size)) { if (vo->opts->fs) { // we will resize as soon as we get out of fullscreen s->out_fs_resize = YES; } else { // only if we are not in fullscreen and the video size did // change we resize the window and set a new aspect ratio - [s->window setContentSize:s->current_video_size - keepCentered:YES]; - [s->window setContentAspectRatio:s->current_video_size]; + resize_window_from_stored_size(vo); } } + + cocoa_set_window_title(vo, vo_get_window_title(vo)); + + resize_window(vo); +} + +static void resize_redraw(struct vo *vo, int width, int height) +{ + struct vo_cocoa_state *s = vo->cocoa; + if (s->resize_redraw) { + vo_cocoa_set_current_context(vo, true); + [s->glContext update]; + s->resize_redraw(vo, width, height); + [s->glContext flushBuffer]; + s->did_async_resize = YES; + vo_cocoa_set_current_context(vo, false); + } } int vo_cocoa_config_window(struct vo *vo, uint32_t d_width, @@ -441,116 +425,153 @@ int vo_cocoa_config_window(struct vo *vo, uint32_t d_width, int gl3profile) { struct vo_cocoa_state *s = vo->cocoa; - struct mp_vo_opts *opts = vo->opts; - - if (vo->config_count > 0) { - NSPoint origin = [s->window frame].origin; - vo->dx = origin.x; - vo->dy = origin.y; - } - - update_state_sizes(s, d_width, d_height); - - if (!(s->window || s->glContext)) { - if (create_window(vo, d_width, d_height, flags, gl3profile) < 0) - return -1; - } else { - update_window(vo); - } - - [s->window setFrameOrigin:NSMakePoint(vo->dx, vo->dy)]; - - if (flags & VOFLAG_HIDDEN) { - [s->window orderOut:nil]; - } else { - [s->window makeKeyAndOrderFront:nil]; - [NSApp activateIgnoringOtherApps:YES]; - } - - if (flags & VOFLAG_FULLSCREEN && !vo->opts->fs) - vo_cocoa_fullscreen(vo); - - vo_set_level(vo, opts->ontop); + __block int ctxok = 0; + s->enable_resize_redraw = NO; + + dispatch_sync(dispatch_get_main_queue(), ^{ + s->aspdat = vo->aspdat; + update_state_sizes(s, d_width, d_height); + + if (flags & VOFLAG_HIDDEN) { + // This is certainly the first execution of vo_config_window and + // is called in order for an OpenGL based VO to perform detection + // of OpenGL extensions. On OSX to accomplish this task we are + // allowed only create a OpenGL context without attaching it to + // a drawable. + ctxok = create_gl_context(vo, gl3profile); + if (ctxok < 0) return; + } else if (!s->glContext || !s->window) { + // Either glContext+Window or Window alone are not created. + // Handle each of them independently. This is to handle correctly + // both VOs like vo_corevideo who skip the the OpenGL detection + // phase completly and generic OpenGL VOs who use VOFLAG_HIDDEN. + if (!s->glContext) { + ctxok = create_gl_context(vo, gl3profile); + if (ctxok < 0) return; + } + + if (!s->window) + create_window(vo, d_width, d_height, flags); + } - resize_window(vo); + if (s->window) { + // Everything is properly initialized + update_window(vo); + } + }); - if (s->window_title) - [s->window_title release]; + if (ctxok < 0) + return ctxok; - s->window_title = - [[NSString alloc] initWithUTF8String:vo_get_window_title(vo)]; - [s->window setTitle: s->window_title]; + [vo->cocoa->glContext makeCurrentContext]; + s->enable_resize_redraw = YES; return 0; } -void vo_cocoa_swap_buffers(struct vo *vo) +static bool resize_callback_registered(struct vo *vo) { struct vo_cocoa_state *s = vo->cocoa; - [s->glContext flushBuffer]; + return s->enable_resize_redraw && !!s->resize_redraw; } -static void vo_cocoa_display_cursor(struct vo *vo, int requested_state) +void vo_cocoa_set_current_context(struct vo *vo, bool current) { struct vo_cocoa_state *s = vo->cocoa; - if (requested_state) { - if (!vo->opts->fs || s->vo_cursor_autohide_delay > -2) { - s->display_cursor = requested_state; - CGDisplayShowCursor(kCGDirectMainDisplay); - } + if (current) { + [s->lock lock]; + [s->glContext makeCurrentContext]; } else { - if (s->vo_cursor_autohide_delay != -1) { - s->display_cursor = requested_state; - CGDisplayHideCursor(kCGDirectMainDisplay); - } + [NSOpenGLContext clearCurrentContext]; + [s->lock unlock]; } } -int vo_cocoa_check_events(struct vo *vo) +void vo_cocoa_swap_buffers(struct vo *vo) { struct vo_cocoa_state *s = vo->cocoa; - - int ms_time = (int) ([[NSProcessInfo processInfo] systemUptime] * 1000); - - // automatically hide mouse cursor - if (vo->opts->fs && s->display_cursor && - (ms_time - s->cursor_timer >= s->vo_cursor_autohide_delay)) { - vo_cocoa_display_cursor(vo, 0); - s->cursor_timer = ms_time; + if (s->did_async_resize && resize_callback_registered(vo)) { + // when in live resize the GL view asynchronously updates itself from + // it's drawRect: implementation and calls flushBuffer. This means the + // backbuffer is probably in an inconsistent state, so we skip one + // flushBuffer call here on the playloop thread. + s->did_async_resize = NO; + } else { + [s->glContext flushBuffer]; } +} - int result = 0; +int vo_cocoa_check_events(struct vo *vo) +{ + struct vo_cocoa_state *s = vo->cocoa; - for (;;) { - NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil - inMode:NSEventTrackingRunLoopMode dequeue:YES]; - if (event == nil) - break; - [NSApp sendEvent:event]; + if (s->did_resize) { + s->did_resize = NO; + resize_window(vo); + return VO_EVENT_RESIZE; + } - if (s->did_resize) { - s->did_resize = NO; - resize_window(vo); - result |= VO_EVENT_RESIZE; - } - // Without SDL's bootstrap code (include SDL.h in mplayer.c), - // on Leopard, we have trouble to get the play window automatically focused - // when the app is actived. The Following code fix this problem. - if ([event type] == NSAppKitDefined - && [event subtype] == NSApplicationActivatedEventType) { - [s->window makeMainWindow]; - [s->window makeKeyAndOrderFront:nil]; - } + if (s->want_redraw) { + s->want_redraw = NO; + vo->want_redraw = true; } - return result; + return 0; } void vo_cocoa_fullscreen(struct vo *vo) { + if (![NSThread isMainThread]) { + // This is the secondary thread, unlock since we are going to invoke a + // method synchronously on the GUI thread using Cocoa. + vo_cocoa_set_current_context(vo, false); + } + struct vo_cocoa_state *s = vo->cocoa; - [s->window fullscreen]; - resize_window(vo); + [s->window performSelectorOnMainThread:@selector(fullscreen) + withObject:nil + waitUntilDone:YES]; + + + if (![NSThread isMainThread]) { + // Now lock again! + vo_cocoa_set_current_context(vo, true); + } +} + +int vo_cocoa_control(struct vo *vo, int *events, int request, void *arg) +{ + switch (request) { + case VOCTRL_CHECK_EVENTS: + *events |= vo_cocoa_check_events(vo); + return VO_TRUE; + case VOCTRL_FULLSCREEN: + vo_cocoa_fullscreen(vo); + *events |= VO_EVENT_RESIZE; + return VO_TRUE; + case VOCTRL_ONTOP: + vo_cocoa_ontop(vo); + return VO_TRUE; + case VOCTRL_UPDATE_SCREENINFO: + vo_cocoa_update_screen_info(vo); + return VO_TRUE; + case VOCTRL_SET_CURSOR_VISIBILITY: { + bool visible = *(bool *)arg; + vo_cocoa_set_cursor_visibility(vo, visible); + return VO_TRUE; + } + case VOCTRL_UPDATE_WINDOW_TITLE: { + cocoa_set_window_title(vo, (const char *) arg); + return VO_TRUE; + } + case VOCTRL_RESTORE_SCREENSAVER: + enable_power_management(vo); + return VO_TRUE; + case VOCTRL_KILL_SCREENSAVER: + disable_power_management(vo); + return VO_TRUE; + } + return VO_NOTIMPL; } int vo_cocoa_swap_interval(int enabled) @@ -587,108 +608,103 @@ int vo_cocoa_cgl_color_size(struct vo *vo) return 8; } -static NSMenuItem *new_menu_item(NSMenu *parent_menu, NSString *title, - SEL action, NSString *key_equivalent) +@implementation GLMPlayerWindow +@synthesize videoOutput = _video_output; +- (void)windowDidResize:(NSNotification *) notification { - NSMenuItem *new_item = - [[NSMenuItem alloc] initWithTitle:title action:action - keyEquivalent:key_equivalent]; - [parent_menu addItem:new_item]; - return [new_item autorelease]; + if (self.videoOutput) { + struct vo_cocoa_state *s = self.videoOutput->cocoa; + s->did_resize = YES; + } } - -static NSMenuItem *new_main_menu_item(NSMenu *parent_menu, NSMenu *child_menu, - NSString *title) +- (void)toggleMissionControlFullScreen:(BOOL)willBeFullscreen { - NSMenuItem *new_item = - [[NSMenuItem alloc] initWithTitle:title action:nil - keyEquivalent:@""]; - [new_item setSubmenu:child_menu]; - [parent_menu addItem:new_item]; - return [new_item autorelease]; -} + struct vo_cocoa_state *s = self.videoOutput->cocoa; -void create_menu() + if (willBeFullscreen) { + [self setContentResizeIncrements:NSMakeSize(1, 1)]; + } else { + [self setContentAspectRatio:s->current_video_size]; + } + + [self toggleFullScreen:nil]; +} +- (void)toggleViewFullscreen:(BOOL)willBeFullscreen { - NSAutoreleasePool *pool = [NSAutoreleasePool new]; - NSMenu *main_menu, *m_menu, *w_menu; - NSMenuItem *app_menu_item; + struct vo_cocoa_state *s = self.videoOutput->cocoa; - main_menu = [[NSMenu new] autorelease]; - app_menu_item = [[NSMenuItem new] autorelease]; - [main_menu addItem:app_menu_item]; - [NSApp setMainMenu: main_menu]; + if (willBeFullscreen) { + NSApplicationPresentationOptions popts = + NSApplicationPresentationDefault; - m_menu = [[[NSMenu alloc] initWithTitle:@"Movie"] autorelease]; - new_menu_item(m_menu, @"Half Size", @selector(halfSize), @"0"); - new_menu_item(m_menu, @"Normal Size", @selector(normalSize), @"1"); - new_menu_item(m_menu, @"Double Size", @selector(doubleSize), @"2"); + if ([s->fs_screen hasMenubar]) + popts |= NSApplicationPresentationAutoHideMenuBar; - new_main_menu_item(main_menu, m_menu, @"Movie"); + if ([s->fs_screen hasDock]) + popts |= NSApplicationPresentationAutoHideDock; - w_menu = [[[NSMenu alloc] initWithTitle:@"Window"] autorelease]; - new_menu_item(w_menu, @"Minimize", @selector(performMiniaturize:), @"m"); - new_menu_item(w_menu, @"Zoom", @selector(performZoom:), @"z"); + NSDictionary *fsopts = @{ + NSFullScreenModeAllScreens : @NO, + NSFullScreenModeApplicationPresentationOptions : @(popts) + }; - new_main_menu_item(main_menu, w_menu, @"Window"); - [pool release]; -} + [s->view enterFullScreenMode:s->fs_screen withOptions:fsopts]; -@implementation GLMPlayerWindow -- (void)setVideoOutput:(struct vo *)vo -{ - _vo = vo; -} + // The original "windowed" window will staty around since sending a + // view fullscreen wraps it in another window. This is noticeable when + // sending the View fullscreen to another screen. Make it go away + // manually. + [s->window orderOut:self]; + } else { + [s->view exitFullScreenModeWithOptions:nil]; -- (void)windowDidResize:(NSNotification *) notification -{ - if (_vo) { - struct vo_cocoa_state *s = _vo->cocoa; - s->did_resize = YES; + // Show the "windowed" window again. + [s->window makeKeyAndOrderFront:self]; + [self makeFirstResponder:s->view]; } } - - (void)fullscreen { - struct vo_cocoa_state *s = _vo->cocoa; - struct mp_vo_opts *opts = _vo->opts; + struct vo_cocoa_state *s = self.videoOutput->cocoa; + struct mp_vo_opts *opts = self.videoOutput->opts; + + vo_cocoa_update_screen_info(self.videoOutput); + + // Go use the fullscreen API selected by the user. View-based or Mission + // Control. + if (opts->native_fs) { + [self toggleMissionControlFullScreen:!opts->fs]; + } else { + [self toggleViewFullscreen:!opts->fs]; + } + + // Do common work such as setting mouse visibility and actually setting + // the new fullscreen state if (!opts->fs) { - update_screen_info(_vo); - if (current_screen_has_dock_or_menubar(_vo)) - [NSApp setPresentationOptions:NSApplicationPresentationHideDock| - NSApplicationPresentationHideMenuBar]; - s->windowed_frame = [self frame]; - [self setHasShadow:NO]; - [self setStyleMask:s->fullscreen_mask]; - [self setFrame:s->fsscreen_frame display:YES animate:NO]; - opts->fs = true; - vo_cocoa_display_cursor(_vo, 0); - [self setMovableByWindowBackground: NO]; + opts->fs = VO_TRUE; + vo_cocoa_set_cursor_visibility(self.videoOutput, false); } else { - [NSApp setPresentationOptions:NSApplicationPresentationDefault]; - [self setHasShadow:YES]; - [self setStyleMask:s->windowed_mask]; - [self setTitle:s->window_title]; - [self setFrame:s->windowed_frame display:YES animate:NO]; - if (s->out_fs_resize) { - [self setContentSize:s->current_video_size keepCentered:YES]; - s->out_fs_resize = NO; - } - [self setContentAspectRatio:s->current_video_size]; - opts->fs = false; - vo_cocoa_display_cursor(_vo, 1); - [self setMovableByWindowBackground: YES]; + opts->fs = VO_FALSE; + vo_cocoa_set_cursor_visibility(self.videoOutput, true); + } + + // Change window size if the core attempted to change it while we were in + // fullscreen. For example config() might have been called as a result of + // a new file changing the window size. + if (!opts->fs && s->out_fs_resize) { + resize_window_from_stored_size(self.videoOutput); + s->out_fs_resize = NO; } + + // Make the core aware of the view size change. + resize_window(self.videoOutput); } - (BOOL)canBecomeMainWindow { return YES; } - (BOOL)canBecomeKeyWindow { return YES; } -- (BOOL)acceptsFirstResponder { return YES; } -- (BOOL)becomeFirstResponder { return YES; } -- (BOOL)resignFirstResponder { return YES; } - (BOOL)windowShouldClose:(id)sender { - mplayer_put_key(_vo->key_fifo, MP_KEY_CLOSE_WIN); + cocoa_put_key(MP_KEY_CLOSE_WIN); // We have to wait for MPlayer to handle this, // otherwise we are in trouble if the // MP_KEY_CLOSE_WIN handler is disabled @@ -697,89 +713,140 @@ void create_menu() - (BOOL)isMovableByWindowBackground { - // this is only valid as a starting value. it will be rewritten in the - // -fullscreen method. - if (_vo) { - return !_vo->opts->fs; + if (self.videoOutput) { + return !self.videoOutput->opts->fs; } else { - return NO; + return YES; } } -- (void)handleQuitEvent:(NSAppleEventDescriptor*)e - withReplyEvent:(NSAppleEventDescriptor*)r -{ - mplayer_put_key(_vo->key_fifo, MP_KEY_CLOSE_WIN); -} - -- (void)keyDown:(NSEvent *)theEvent -{ - NSString *chars; +- (void)normalSize { [self mulSize:1.0f]; } - if (RightAltPressed(theEvent)) - chars = [theEvent characters]; - else - chars = [theEvent charactersIgnoringModifiers]; +- (void)halfSize { [self mulSize:0.5f];} - int key = convert_key([theEvent keyCode], *[chars UTF8String]); +- (void)doubleSize { [self mulSize:2.0f];} - if (key > -1) { - if ([theEvent modifierFlags] & NSShiftKeyMask) - key |= MP_KEY_MODIFIER_SHIFT; - if ([theEvent modifierFlags] & NSControlKeyMask) - key |= MP_KEY_MODIFIER_CTRL; - if (LeftAltPressed(theEvent)) - key |= MP_KEY_MODIFIER_ALT; - if ([theEvent modifierFlags] & NSCommandKeyMask) - key |= MP_KEY_MODIFIER_META; - mplayer_put_key(_vo->key_fifo, key); +- (void)mulSize:(float)multiplier +{ + if (!self.videoOutput->opts->fs) { + NSSize size = { + .width = self.videoOutput->cocoa->aspdat.prew * multiplier, + .height = self.videoOutput->cocoa->aspdat.preh * multiplier + }; + [self setContentSize:size keepCentered:YES]; } } -- (void)mouseMoved: (NSEvent *) theEvent +- (int)titleHeight { - if (_vo->opts->fs) - vo_cocoa_display_cursor(_vo, 1); + NSRect of = [self frame]; + NSRect cb = [[self contentView] bounds]; + return of.size.height - cb.size.height; } -- (void)mouseDragged:(NSEvent *)theEvent +- (NSRect)clipFrame:(NSRect)frame withContentAspect:(NSSize) aspect { - [self mouseEvent: theEvent]; -} + NSRect vf = [[self screen] visibleFrame]; + double ratio = (double)aspect.width / (double)aspect.height; -- (void)mouseDown:(NSEvent *)theEvent -{ - [self mouseEvent: theEvent]; + // clip frame to screens visibile frame + frame = CGRectIntersection(frame, |