2012-03-04 21:04:07 +00:00
|
|
|
/*
|
|
|
|
* Cocoa OpenGL Backend
|
|
|
|
*
|
2013-09-03 19:18:28 +00:00
|
|
|
* This file is part of mpv.
|
2012-03-04 21:04:07 +00:00
|
|
|
*
|
2013-09-03 19:18:28 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
2012-03-04 21:04:07 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2013-09-03 19:18:28 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2012-03-04 21:04:07 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2013-09-03 19:18:28 +00:00
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2012-03-04 21:04:07 +00:00
|
|
|
*/
|
|
|
|
|
2011-10-15 16:44:00 +00:00
|
|
|
#import <Cocoa/Cocoa.h>
|
2012-08-07 08:31:39 +00:00
|
|
|
#import <CoreServices/CoreServices.h> // for CGDisplayHideCursor
|
2012-09-16 18:53:04 +00:00
|
|
|
#import <IOKit/pwr_mgt/IOPMLib.h>
|
2012-04-14 13:02:28 +00:00
|
|
|
#include <dlfcn.h>
|
|
|
|
|
2011-10-15 16:44:00 +00:00
|
|
|
#include "cocoa_common.h"
|
2013-09-03 19:18:28 +00:00
|
|
|
#include "video/out/cocoa/window.h"
|
|
|
|
#include "video/out/cocoa/view.h"
|
|
|
|
#import "video/out/cocoa/mpvadapter.h"
|
|
|
|
|
2013-10-05 12:39:27 +00:00
|
|
|
#include "osdep/macosx_compat.h"
|
2013-09-03 19:18:28 +00:00
|
|
|
#include "osdep/macosx_application.h"
|
|
|
|
#include "osdep/macosx_events.h"
|
2011-10-15 16:44:00 +00:00
|
|
|
|
2012-07-21 08:37:13 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2013-08-06 20:41:30 +00:00
|
|
|
#include "mpvcore/options.h"
|
2013-09-03 19:18:28 +00:00
|
|
|
#include "video/out/vo.h"
|
|
|
|
#include "video/out/aspect.h"
|
2011-10-15 16:44:00 +00:00
|
|
|
|
2013-08-06 20:41:30 +00:00
|
|
|
#include "mpvcore/input/input.h"
|
2011-10-15 16:44:00 +00:00
|
|
|
#include "talloc.h"
|
|
|
|
|
2013-08-06 20:41:30 +00:00
|
|
|
#include "mpvcore/mp_msg.h"
|
2011-10-15 16:44:00 +00:00
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
static void vo_cocoa_fullscreen(struct vo *vo);
|
|
|
|
static void vo_cocoa_ontop(struct vo *vo);
|
2013-07-21 08:33:18 +00:00
|
|
|
|
2011-10-15 16:44:00 +00:00
|
|
|
struct vo_cocoa_state {
|
2013-09-03 19:18:28 +00:00
|
|
|
MpvVideoWindow *window;
|
|
|
|
MpvVideoView *view;
|
|
|
|
NSOpenGLContext *gl_ctx;
|
|
|
|
NSOpenGLPixelFormat *gl_pixfmt;
|
2011-10-15 16:44:00 +00:00
|
|
|
|
2013-05-28 22:14:34 +00:00
|
|
|
NSScreen *current_screen;
|
|
|
|
NSScreen *fs_screen;
|
2011-10-15 16:44:00 +00:00
|
|
|
|
2012-09-29 22:32:53 +00:00
|
|
|
NSInteger window_level;
|
2011-12-07 11:42:04 +00:00
|
|
|
|
2013-03-04 13:23:06 +00:00
|
|
|
struct aspect_data aspdat;
|
|
|
|
|
2011-10-15 16:44:00 +00:00
|
|
|
bool did_resize;
|
2013-09-03 19:18:28 +00:00
|
|
|
bool skip_next_swap_buffer;
|
|
|
|
bool inside_sync_section;
|
2012-09-16 18:53:04 +00:00
|
|
|
|
|
|
|
IOPMAssertionID power_mgmt_assertion;
|
2013-01-20 16:15:37 +00:00
|
|
|
|
2013-05-13 21:25:35 +00:00
|
|
|
NSLock *lock;
|
2013-05-12 21:51:58 +00:00
|
|
|
bool enable_resize_redraw;
|
2013-09-28 11:51:29 +00:00
|
|
|
void *ctx;
|
|
|
|
void (*gl_clear)(void *ctx);
|
2013-03-04 13:23:06 +00:00
|
|
|
void (*resize_redraw)(struct vo *vo, int w, int h);
|
2013-08-01 06:23:53 +00:00
|
|
|
|
|
|
|
struct mp_log *log;
|
2013-03-04 13:23:06 +00:00
|
|
|
};
|
2011-10-15 16:44:00 +00:00
|
|
|
|
2012-04-14 13:02:28 +00:00
|
|
|
void *vo_cocoa_glgetaddr(const char *s)
|
|
|
|
{
|
|
|
|
void *ret = NULL;
|
|
|
|
void *handle = dlopen(
|
|
|
|
"/System/Library/Frameworks/OpenGL.framework/OpenGL",
|
|
|
|
RTLD_LAZY | RTLD_LOCAL);
|
|
|
|
if (!handle)
|
|
|
|
return NULL;
|
|
|
|
ret = dlsym(handle, s);
|
|
|
|
dlclose(handle);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-09-16 18:53:04 +00:00
|
|
|
static void enable_power_management(struct vo *vo)
|
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
|
|
|
if (!s->power_mgmt_assertion) return;
|
|
|
|
IOPMAssertionRelease(s->power_mgmt_assertion);
|
|
|
|
s->power_mgmt_assertion = kIOPMNullAssertionID;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void disable_power_management(struct vo *vo)
|
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
|
|
|
if (s->power_mgmt_assertion) return;
|
2013-05-28 14:53:17 +00:00
|
|
|
IOPMAssertionCreateWithName(
|
|
|
|
kIOPMAssertionTypePreventUserIdleDisplaySleep,
|
|
|
|
kIOPMAssertionLevelOn,
|
2013-05-28 16:00:34 +00:00
|
|
|
CFSTR("io.mpv.video_playing_back"),
|
2013-05-28 14:53:17 +00:00
|
|
|
&s->power_mgmt_assertion);
|
2012-09-16 18:53:04 +00:00
|
|
|
}
|
|
|
|
|
2011-10-15 16:44:00 +00:00
|
|
|
int vo_cocoa_init(struct vo *vo)
|
|
|
|
{
|
2013-09-03 19:18:28 +00:00
|
|
|
struct vo_cocoa_state *s = talloc_zero(vo, struct vo_cocoa_state);
|
|
|
|
*s = (struct vo_cocoa_state){
|
|
|
|
.did_resize = false,
|
|
|
|
.skip_next_swap_buffer = false,
|
|
|
|
.inside_sync_section = false,
|
|
|
|
.power_mgmt_assertion = kIOPMNullAssertionID,
|
|
|
|
.lock = [[NSLock alloc] init],
|
|
|
|
.enable_resize_redraw = NO,
|
|
|
|
.log = mp_log_new(s, vo->log, "cocoa"),
|
|
|
|
};
|
|
|
|
vo->cocoa = s;
|
2011-10-15 16:44:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-09-18 06:34:28 +00:00
|
|
|
static void vo_cocoa_set_cursor_visibility(struct vo *vo, bool *visible)
|
2013-05-16 22:29:42 +00:00
|
|
|
{
|
2013-05-31 22:35:46 +00:00
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
|
|
|
|
2013-09-18 06:34:28 +00:00
|
|
|
if (*visible) {
|
2013-05-16 22:29:42 +00:00
|
|
|
CGDisplayShowCursor(kCGDirectMainDisplay);
|
2013-09-02 06:21:03 +00:00
|
|
|
} else if ([s->view canHideCursor]) {
|
2013-05-16 22:29:42 +00:00
|
|
|
CGDisplayHideCursor(kCGDirectMainDisplay);
|
2013-09-18 06:34:28 +00:00
|
|
|
} else {
|
|
|
|
*visible = true;
|
2013-05-16 22:29:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-15 16:44:00 +00:00
|
|
|
void vo_cocoa_uninit(struct vo *vo)
|
|
|
|
{
|
2013-11-26 18:01:52 +00:00
|
|
|
vo_cocoa_set_current_context(vo, false);
|
2013-03-04 13:23:06 +00:00
|
|
|
dispatch_sync(dispatch_get_main_queue(), ^{
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
|
|
|
enable_power_management(vo);
|
|
|
|
[NSApp setPresentationOptions:NSApplicationPresentationDefault];
|
2011-10-15 16:44:00 +00:00
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
// XXX: It looks like there are some circular retain cycles for the
|
|
|
|
// video view / video window that cause them to not be deallocated,
|
|
|
|
// This is a workaround to make the fullscreen window be released,
|
|
|
|
// but the retain cycle problems should be investigated.
|
|
|
|
if ([s->view isInFullScreenMode])
|
2013-06-29 20:51:32 +00:00
|
|
|
[[s->view window] release];
|
|
|
|
|
2013-03-04 13:23:06 +00:00
|
|
|
[s->window release];
|
|
|
|
s->window = nil;
|
2013-09-03 19:18:28 +00:00
|
|
|
|
|
|
|
[s->gl_ctx release];
|
|
|
|
s->gl_ctx = nil;
|
|
|
|
|
|
|
|
[s->lock release];
|
|
|
|
s->lock = nil;
|
2013-03-04 13:23:06 +00:00
|
|
|
});
|
2011-10-15 16:44:00 +00:00
|
|
|
}
|
|
|
|
|
2013-03-04 13:23:06 +00:00
|
|
|
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;
|
|
|
|
s->resize_redraw = cb;
|
|
|
|
}
|
|
|
|
|
2013-09-28 11:51:29 +00:00
|
|
|
void vo_cocoa_register_gl_clear_callback(struct vo *vo, void *ctx,
|
|
|
|
void (*cb)(void *ctx))
|
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
|
|
|
s->ctx = ctx;
|
|
|
|
s->gl_clear = cb;
|
|
|
|
}
|
|
|
|
|
2013-08-01 06:23:53 +00:00
|
|
|
static int get_screen_handle(struct vo *vo, int identifier, NSWindow *window,
|
|
|
|
NSScreen **screen) {
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2013-02-17 15:35:44 +00:00
|
|
|
NSArray *screens = [NSScreen screens];
|
|
|
|
int n_of_displays = [screens count];
|
|
|
|
|
|
|
|
if (identifier >= n_of_displays) { // check if the identifier is out of bounds
|
2013-08-01 06:23:53 +00:00
|
|
|
MP_INFO(s, "Screen ID %d does not exist, falling back to main "
|
|
|
|
"device\n", identifier);
|
2013-02-17 15:35:44 +00:00
|
|
|
identifier = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (identifier < 0) {
|
|
|
|
// default behaviour gets either the window screen or the main screen
|
|
|
|
// if window is not available
|
|
|
|
if (! (*screen = [window screen]) )
|
|
|
|
*screen = [screens objectAtIndex:0];
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
*screen = [screens objectAtIndex:(identifier)];
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
static void vo_cocoa_update_screens_pointers(struct vo *vo)
|
2011-10-15 16:44:00 +00:00
|
|
|
{
|
2012-09-13 07:32:59 +00:00
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2013-03-04 21:41:27 +00:00
|
|
|
struct mp_vo_opts *opts = vo->opts;
|
2013-08-01 06:23:53 +00:00
|
|
|
get_screen_handle(vo, opts->screen_id, s->window, &s->current_screen);
|
|
|
|
get_screen_handle(vo, opts->fsscreen_id, s->window, &s->fs_screen);
|
2011-10-15 16:44:00 +00:00
|
|
|
}
|
|
|
|
|
2013-05-28 22:14:34 +00:00
|
|
|
static void vo_cocoa_update_screen_info(struct vo *vo)
|
2011-10-15 16:44:00 +00:00
|
|
|
{
|
2012-09-13 07:32:59 +00:00
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2013-03-04 21:41:27 +00:00
|
|
|
struct mp_vo_opts *opts = vo->opts;
|
2012-11-10 19:19:21 +00:00
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
vo_cocoa_update_screens_pointers(vo);
|
2013-05-28 22:14:34 +00:00
|
|
|
|
|
|
|
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;
|
2011-10-15 16:44:00 +00:00
|
|
|
}
|
|
|
|
|
2012-09-13 07:32:59 +00:00
|
|
|
static void resize_window(struct vo *vo)
|
2011-10-15 16:44:00 +00:00
|
|
|
{
|
2012-09-13 07:32:59 +00:00
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2013-09-03 19:18:28 +00:00
|
|
|
NSRect frame = [s->view frameInPixels];
|
2012-08-07 08:31:39 +00:00
|
|
|
vo->dwidth = frame.size.width;
|
|
|
|
vo->dheight = frame.size.height;
|
2013-09-03 19:18:28 +00:00
|
|
|
[s->gl_ctx update];
|
2011-10-15 16:44:00 +00:00
|
|
|
}
|
|
|
|
|
2012-09-13 07:32:59 +00:00
|
|
|
static void vo_set_level(struct vo *vo, int ontop)
|
2011-12-07 11:42:04 +00:00
|
|
|
{
|
2012-09-13 07:32:59 +00:00
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2011-12-07 11:42:04 +00:00
|
|
|
if (ontop) {
|
2013-10-12 18:57:28 +00:00
|
|
|
// +1 is not enough as that will show the icon layer on top of the
|
|
|
|
// menubar when the application is not frontmost. so use +2
|
|
|
|
s->window_level = NSMainMenuWindowLevel + 2;
|
2011-12-07 11:42:04 +00:00
|
|
|
} else {
|
2012-09-29 22:32:53 +00:00
|
|
|
s->window_level = NSNormalWindowLevel;
|
2011-12-07 11:42:04 +00:00
|
|
|
}
|
|
|
|
|
2013-06-11 19:20:46 +00:00
|
|
|
[[s->view window] setLevel:s->window_level];
|
|
|
|
[s->window setLevel:s->window_level];
|
2011-12-07 11:42:04 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
static void vo_cocoa_ontop(struct vo *vo)
|
2011-12-07 11:42:04 +00:00
|
|
|
{
|
2013-03-04 21:41:27 +00:00
|
|
|
struct mp_vo_opts *opts = vo->opts;
|
|
|
|
opts->ontop = !opts->ontop;
|
|
|
|
vo_set_level(vo, opts->ontop);
|
2011-12-07 11:42:04 +00:00
|
|
|
}
|
|
|
|
|
2013-06-01 09:26:06 +00:00
|
|
|
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;
|
|
|
|
struct mp_vo_opts *opts = vo->opts;
|
|
|
|
|
|
|
|
const NSRect contentRect = NSMakeRect(0, 0, d_width, d_height);
|
2012-10-15 20:03:08 +00:00
|
|
|
|
2013-05-28 22:14:34 +00:00
|
|
|
int window_mask = 0;
|
2013-06-01 09:26:06 +00:00
|
|
|
if (opts->border) {
|
2013-05-28 22:14:34 +00:00
|
|
|
window_mask = NSTitledWindowMask|NSClosableWindowMask|
|
|
|
|
NSMiniaturizableWindowMask|NSResizableWindowMask;
|
|
|
|
} else {
|
|
|
|
window_mask = NSBorderlessWindowMask;
|
|
|
|
}
|
|
|
|
|
2012-10-15 20:03:08 +00:00
|
|
|
s->window =
|
2013-09-03 19:18:28 +00:00
|
|
|
[[MpvVideoWindow alloc] initWithContentRect:contentRect
|
|
|
|
styleMask:window_mask
|
|
|
|
backing:NSBackingStoreBuffered
|
|
|
|
defer:NO];
|
|
|
|
s->view = [[[MpvVideoView alloc] initWithFrame:contentRect] autorelease];
|
2012-10-15 20:03:08 +00:00
|
|
|
|
2013-05-28 22:14:34 +00:00
|
|
|
[s->view setWantsBestResolutionOpenGLSurface:YES];
|
2012-10-15 20:03:08 +00:00
|
|
|
|
2013-02-23 17:28:22 +00:00
|
|
|
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:));
|
2012-10-15 20:03:08 +00:00
|
|
|
|
2013-02-23 17:28:22 +00:00
|
|
|
[s->window setRestorable:NO];
|
2013-05-28 22:14:34 +00:00
|
|
|
[s->window setContentView:s->view];
|
2013-09-03 19:18:28 +00:00
|
|
|
[s->gl_ctx setView:s->view];
|
|
|
|
|
|
|
|
MpvCocoaAdapter *adapter = [[[MpvCocoaAdapter alloc] init] autorelease];
|
|
|
|
adapter.vout = vo;
|
|
|
|
s->view.adapter = adapter;
|
|
|
|
s->window.adapter = adapter;
|
2012-10-15 20:03:08 +00:00
|
|
|
|
|
|
|
[s->window setDelegate:s->window];
|
2013-02-23 17:28:22 +00:00
|
|
|
[s->window makeMainWindow];
|
2012-10-15 20:03:08 +00:00
|
|
|
|
2013-06-01 09:26:06 +00:00
|
|
|
[s->window setFrameOrigin:NSMakePoint(vo->dx, vo->dy)];
|
|
|
|
[s->window makeKeyAndOrderFront:nil];
|
|
|
|
[NSApp activateIgnoringOtherApps:YES];
|
2013-03-15 19:51:32 +00:00
|
|
|
|
2013-06-01 09:26:06 +00:00
|
|
|
vo_set_level(vo, opts->ontop);
|
|
|
|
|
|
|
|
if (opts->native_fs) {
|
|
|
|
[s->window setCollectionBehavior:
|
|
|
|
NSWindowCollectionBehaviorFullScreenPrimary];
|
|
|
|
[NSApp setPresentationOptions:NSFullScreenWindowMask];
|
|
|
|
}
|
2012-10-15 20:03:08 +00:00
|
|
|
}
|
|
|
|
|
2013-06-01 09:26:06 +00:00
|
|
|
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)
|
2013-05-28 22:14:34 +00:00
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2013-06-01 09:26:06 +00:00
|
|
|
|
|
|
|
NSOpenGLPixelFormatAttribute attr[] = {
|
|
|
|
NSOpenGLPFAOpenGLProfile,
|
|
|
|
get_nsopengl_profile(gl3profile),
|
|
|
|
NSOpenGLPFADoubleBuffer,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
s->gl_pixfmt =
|
2013-06-01 09:26:06 +00:00
|
|
|
[[[NSOpenGLPixelFormat alloc] initWithAttributes:attr] autorelease];
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
if (!s->gl_pixfmt) {
|
2013-08-01 06:23:53 +00:00
|
|
|
MP_ERR(s, "Trying to build invalid OpenGL pixel format\n");
|
2013-06-01 09:26:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
s->gl_ctx =
|
|
|
|
[[NSOpenGLContext alloc] initWithFormat:s->gl_pixfmt
|
2013-06-01 09:26:06 +00:00
|
|
|
shareContext:nil];
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
[s->gl_ctx makeCurrentContext];
|
2013-06-01 09:26:06 +00:00
|
|
|
|
|
|
|
return 0;
|
2013-05-28 22:14:34 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 08:57:36 +00:00
|
|
|
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]];
|
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
static void update_window(struct vo *vo, int d_width, int d_height)
|
2012-10-15 20:03:08 +00:00
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2013-09-03 19:18:28 +00:00
|
|
|
[s->window queueNewVideoSize:NSMakeSize(d_width, d_height)];
|
2013-06-16 08:57:36 +00:00
|
|
|
cocoa_set_window_title(vo, vo_get_window_title(vo));
|
2013-07-18 12:05:39 +00:00
|
|
|
vo_cocoa_fullscreen(vo);
|
2012-10-15 20:03:08 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
static void vo_cocoa_resize_redraw(struct vo *vo, int width, int height)
|
2013-03-04 13:23:06 +00:00
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2013-09-03 19:18:28 +00:00
|
|
|
|
|
|
|
if (!s->resize_redraw)
|
|
|
|
return;
|
|
|
|
|
2013-09-28 12:53:55 +00:00
|
|
|
vo_cocoa_set_current_context(vo, true);
|
2013-09-03 19:18:28 +00:00
|
|
|
|
|
|
|
[s->gl_ctx update];
|
|
|
|
|
|
|
|
if (s->enable_resize_redraw) {
|
2013-03-04 13:23:06 +00:00
|
|
|
s->resize_redraw(vo, width, height);
|
2013-09-03 19:18:28 +00:00
|
|
|
s->skip_next_swap_buffer = true;
|
|
|
|
} else {
|
2013-09-28 11:51:29 +00:00
|
|
|
s->gl_clear(s->ctx);
|
2013-03-04 13:23:06 +00:00
|
|
|
}
|
2013-09-03 19:18:28 +00:00
|
|
|
|
|
|
|
[s->gl_ctx flushBuffer];
|
2013-09-28 12:53:55 +00:00
|
|
|
vo_cocoa_set_current_context(vo, false);
|
2013-03-04 13:23:06 +00:00
|
|
|
}
|
|
|
|
|
2013-02-24 22:32:51 +00:00
|
|
|
int vo_cocoa_config_window(struct vo *vo, uint32_t d_width,
|
2012-10-15 20:03:08 +00:00
|
|
|
uint32_t d_height, uint32_t flags,
|
|
|
|
int gl3profile)
|
|
|
|
{
|
2013-05-12 21:51:58 +00:00
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2013-06-01 09:26:06 +00:00
|
|
|
__block int ctxok = 0;
|
2013-03-03 20:29:32 +00:00
|
|
|
|
2013-05-12 21:51:58 +00:00
|
|
|
dispatch_sync(dispatch_get_main_queue(), ^{
|
2013-09-03 19:18:28 +00:00
|
|
|
s->inside_sync_section = true;
|
|
|
|
s->enable_resize_redraw = false;
|
2013-03-04 13:23:06 +00:00
|
|
|
s->aspdat = vo->aspdat;
|
2011-12-07 11:42:04 +00:00
|
|
|
|
2013-06-01 09:26:06 +00:00
|
|
|
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;
|
2013-09-03 19:18:28 +00:00
|
|
|
} else if (!s->gl_ctx || !s->window) {
|
|
|
|
// Either gl_ctx+window or window alone is not created.
|
2013-06-01 09:26:06 +00:00
|
|
|
// 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.
|
2013-09-03 19:18:28 +00:00
|
|
|
if (!s->gl_ctx) {
|
2013-06-01 09:26:06 +00:00
|
|
|
ctxok = create_gl_context(vo, gl3profile);
|
|
|
|
if (ctxok < 0) return;
|
2013-05-29 20:30:05 +00:00
|
|
|
}
|
2011-10-15 16:44:00 +00:00
|
|
|
|
2013-06-01 09:26:06 +00:00
|
|
|
if (!s->window)
|
|
|
|
create_window(vo, d_width, d_height, flags);
|
2013-03-04 13:23:06 +00:00
|
|
|
}
|
2013-03-03 20:29:32 +00:00
|
|
|
|
2013-06-01 09:26:06 +00:00
|
|
|
if (s->window) {
|
|
|
|
// Everything is properly initialized
|
2013-09-03 19:18:28 +00:00
|
|
|
update_window(vo, d_width, d_height);
|
2013-05-10 22:22:23 +00:00
|
|
|
}
|
2013-03-04 13:23:06 +00:00
|
|
|
});
|
2011-10-15 16:44:00 +00:00
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
dispatch_sync(dispatch_get_main_queue(), ^{
|
|
|
|
s->inside_sync_section = false;
|
|
|
|
s->enable_resize_redraw = true;
|
|
|
|
});
|
|
|
|
|
2013-06-01 09:26:06 +00:00
|
|
|
if (ctxok < 0)
|
|
|
|
return ctxok;
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
[vo->cocoa->gl_ctx makeCurrentContext];
|
2011-10-15 16:44:00 +00:00
|
|
|
|
2013-06-01 09:26:06 +00:00
|
|
|
return 0;
|
2013-03-04 13:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void vo_cocoa_set_current_context(struct vo *vo, bool current)
|
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2013-09-28 12:53:55 +00:00
|
|
|
|
2013-03-04 13:23:06 +00:00
|
|
|
if (current) {
|
2013-10-20 19:21:24 +00:00
|
|
|
if (!s->inside_sync_section)
|
|
|
|
[s->lock lock];
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
[s->gl_ctx makeCurrentContext];
|
2013-03-04 13:23:06 +00:00
|
|
|
} else {
|
2013-11-26 18:01:52 +00:00
|
|
|
const bool locked = !![NSOpenGLContext currentContext];
|
2013-03-04 13:23:06 +00:00
|
|
|
[NSOpenGLContext clearCurrentContext];
|
2013-10-20 19:21:24 +00:00
|
|
|
|
2013-11-26 18:01:52 +00:00
|
|
|
if (!s->inside_sync_section && locked)
|
2013-10-20 19:21:24 +00:00
|
|
|
[s->lock unlock];
|
2013-03-04 13:23:06 +00:00
|
|
|
}
|
2011-10-15 16:44:00 +00:00
|
|
|
}
|
|
|
|
|
2012-09-13 07:32:59 +00:00
|
|
|
void vo_cocoa_swap_buffers(struct vo *vo)
|
2011-10-15 16:44:00 +00:00
|
|
|
{
|
2012-09-13 07:32:59 +00:00
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2013-09-03 19:18:28 +00:00
|
|
|
if (s->skip_next_swap_buffer) {
|
|
|
|
// When in live resize the GL view asynchronously updates itself from
|
2013-03-04 13:23:06 +00:00
|
|
|
// 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.
|
2013-09-03 19:18:28 +00:00
|
|
|
s->skip_next_swap_buffer = false;
|
2013-03-04 13:23:06 +00:00
|
|
|
} else {
|
2013-09-03 19:18:28 +00:00
|
|
|
[s->gl_ctx flushBuffer];
|
2013-03-04 13:23:06 +00:00
|
|
|
}
|
2011-10-15 16:44:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int vo_cocoa_check_events(struct vo *vo)
|
|
|
|
{
|
2012-09-13 07:32:59 +00:00
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2013-01-20 15:53:51 +00:00
|
|
|
|
2013-02-23 17:28:22 +00:00
|
|
|
if (s->did_resize) {
|
2013-09-03 19:18:28 +00:00
|
|
|
s->did_resize = false;
|
2013-02-23 17:28:22 +00:00
|
|
|
resize_window(vo);
|
|
|
|
return VO_EVENT_RESIZE;
|
2011-10-15 16:44:00 +00:00
|
|
|
}
|
2013-01-20 15:53:51 +00:00
|
|
|
|
2013-02-23 17:28:22 +00:00
|
|
|
return 0;
|
2011-10-15 16:44:00 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
static void vo_cocoa_fullscreen_sync(struct vo *vo)
|
|
|
|
{
|
|
|
|
dispatch_sync(dispatch_get_main_queue(), ^{
|
|
|
|
vo->cocoa->inside_sync_section = true;
|
|
|
|
vo->cocoa->enable_resize_redraw = false;
|
|
|
|
|
|
|
|
vo_cocoa_fullscreen(vo);
|
|
|
|
|
|
|
|
vo->cocoa->enable_resize_redraw = true;
|
|
|
|
vo->cocoa->inside_sync_section = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vo_cocoa_fullscreen(struct vo *vo)
|
2011-10-15 16:44:00 +00:00
|
|
|
{
|
2012-09-13 07:32:59 +00:00
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2013-09-03 19:18:28 +00:00
|
|
|
struct mp_vo_opts *opts = vo->opts;
|
|
|
|
|
|
|
|
vo_cocoa_update_screen_info(vo);
|
|
|
|
|
|
|
|
if (opts->native_fs) {
|
|
|
|
[s->window setFullScreen:opts->fullscreen];
|
|
|
|
} else {
|
|
|
|
[s->view setFullScreen:opts->fullscreen];
|
|
|
|
}
|
|
|
|
|
|
|
|
[s->window didChangeFullScreenState];
|
|
|
|
|
|
|
|
// Make the core aware of the view size change.
|
|
|
|
resize_window(vo);
|
2011-10-15 16:44:00 +00:00
|
|
|
}
|
|
|
|
|
2013-05-15 16:17:18 +00:00
|
|
|
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:
|
2013-09-03 19:18:28 +00:00
|
|
|
vo_cocoa_fullscreen_sync(vo);
|
2013-05-15 16:17:18 +00:00
|
|
|
*events |= VO_EVENT_RESIZE;
|
|
|
|
return VO_TRUE;
|
|
|
|
case VOCTRL_ONTOP:
|
|
|
|
vo_cocoa_ontop(vo);
|
|
|
|
return VO_TRUE;
|
|
|
|
case VOCTRL_UPDATE_SCREENINFO:
|
2013-05-28 22:14:34 +00:00
|
|
|
vo_cocoa_update_screen_info(vo);
|
2013-05-15 16:17:18 +00:00
|
|
|
return VO_TRUE;
|
2013-11-22 07:41:34 +00:00
|
|
|
case VOCTRL_GET_WINDOW_SIZE: {
|
|
|
|
int *s = arg;
|
|
|
|
vo->cocoa->inside_sync_section = true;
|
|
|
|
dispatch_sync(dispatch_get_main_queue(), ^{
|
|
|
|
NSSize size = [vo->cocoa->view frame].size;
|
|
|
|
s[0] = size.width;
|
|
|
|
s[1] = size.height;
|
|
|
|
});
|
|
|
|
vo->cocoa->inside_sync_section = false;
|
|
|
|
return VO_TRUE;
|
|
|
|
}
|
|
|
|
case VOCTRL_SET_WINDOW_SIZE: {
|
|
|
|
vo->cocoa->inside_sync_section = true;
|
|
|
|
dispatch_sync(dispatch_get_main_queue(), ^{
|
|
|
|
int *s = arg;
|
|
|
|
[vo->cocoa->window queueNewVideoSize:(NSSize){s[0], s[1]}];
|
|
|
|
});
|
|
|
|
vo->cocoa->inside_sync_section = false;
|
|
|
|
return VO_TRUE;
|
|
|
|
}
|
2013-09-08 07:16:19 +00:00
|
|
|
case VOCTRL_SET_CURSOR_VISIBILITY:
|
2013-09-18 06:34:28 +00:00
|
|
|
vo_cocoa_set_cursor_visibility(vo, arg);
|
2013-05-16 22:29:42 +00:00
|
|
|
return VO_TRUE;
|
2013-09-03 19:18:28 +00:00
|
|
|
case VOCTRL_UPDATE_WINDOW_TITLE:
|
2013-06-16 08:57:36 +00:00
|
|
|
cocoa_set_window_title(vo, (const char *) arg);
|
|
|
|
return VO_TRUE;
|
2013-06-13 22:03:32 +00:00
|
|
|
case VOCTRL_RESTORE_SCREENSAVER:
|
2013-06-14 05:34:47 +00:00
|
|
|
enable_power_management(vo);
|
2013-05-15 16:17:18 +00:00
|
|
|
return VO_TRUE;
|
2013-06-13 22:03:32 +00:00
|
|
|
case VOCTRL_KILL_SCREENSAVER:
|
2013-06-14 05:34:47 +00:00
|
|
|
disable_power_management(vo);
|
2013-05-15 16:17:18 +00:00
|
|
|
return VO_TRUE;
|
|
|
|
}
|
|
|
|
return VO_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
2011-12-04 20:53:43 +00:00
|
|
|
int vo_cocoa_swap_interval(int enabled)
|
|
|
|
{
|
2012-09-13 07:32:59 +00:00
|
|
|
[[NSOpenGLContext currentContext] setValues:&enabled
|
|
|
|
forParameter:NSOpenGLCPSwapInterval];
|
2011-12-04 20:53:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-13 07:32:59 +00:00
|
|
|
void *vo_cocoa_cgl_context(struct vo *vo)
|
2011-12-11 11:26:00 +00:00
|
|
|
{
|
2012-09-13 07:32:59 +00:00
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2013-09-03 19:18:28 +00:00
|
|
|
return [s->gl_ctx CGLContextObj];
|
2011-12-11 11:26:00 +00:00
|
|
|
}
|
|
|
|
|
2012-09-13 07:32:59 +00:00
|
|
|
void *vo_cocoa_cgl_pixel_format(struct vo *vo)
|
2011-12-11 11:26:00 +00:00
|
|
|
{
|
2012-09-13 07:32:59 +00:00
|
|
|
return CGLGetPixelFormat(vo_cocoa_cgl_context(vo));
|
2011-12-11 11:26:00 +00:00
|
|
|
}
|
|
|
|
|
2012-09-13 07:32:59 +00:00
|
|
|
int vo_cocoa_cgl_color_size(struct vo *vo)
|
2012-05-14 14:33:06 +00:00
|
|
|
{
|
|
|
|
GLint value;
|
2012-09-13 07:32:59 +00:00
|
|
|
CGLDescribePixelFormat(vo_cocoa_cgl_pixel_format(vo), 0,
|
2012-05-14 14:33:06 +00:00
|
|
|
kCGLPFAColorSize, &value);
|
|
|
|
switch (value) {
|
|
|
|
case 32:
|
|
|
|
case 24:
|
|
|
|
return 8;
|
|
|
|
case 16:
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
@implementation MpvCocoaAdapter
|
|
|
|
@synthesize vout = _video_output;
|
2013-07-20 09:22:50 +00:00
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
- (void)setNeedsResize {
|
|
|
|
struct vo_cocoa_state *s = self.vout->cocoa;
|
|
|
|
s->did_resize = true;
|
2013-09-02 06:21:03 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
- (void)recalcMovableByWindowBackground:(NSPoint)p
|
2013-06-22 08:42:50 +00:00
|
|
|
{
|
2013-09-03 19:18:28 +00:00
|
|
|
BOOL movable = NO;
|
|
|
|
if (![self isInFullScreenMode]) {
|
|
|
|
movable = !mp_input_test_dragging(self.vout->input_ctx, p.x, p.y);
|
2013-06-22 08:42:50 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
[self.vout->cocoa->window setMovableByWindowBackground:movable];
|
2011-12-08 10:17:59 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
- (void)signalMouseMovement:(NSPoint)point {
|
|
|
|
vo_mouse_movement(self.vout, point.x, point.y);
|
|
|
|
[self recalcMovableByWindowBackground:point];
|
2011-10-15 16:44:00 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
- (void)putKey:(int)mpkey withModifiers:(int)modifiers
|
2013-07-21 08:33:18 +00:00
|
|
|
{
|
2013-09-03 19:18:28 +00:00
|
|
|
cocoa_put_key_with_modifiers(mpkey, modifiers);
|
2013-07-21 08:33:18 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
- (void)putAxis:(int)mpkey delta:(float)delta;
|
2013-07-21 08:33:18 +00:00
|
|
|
{
|
2013-09-03 19:18:28 +00:00
|
|
|
mp_input_put_axis(self.vout->input_ctx, mpkey, delta);
|
2013-07-21 08:33:18 +00:00
|
|
|
}
|
|
|
|
|
2013-11-22 07:43:02 +00:00
|
|
|
- (void)putCommand:(char*)cmd
|
|
|
|
{
|
|
|
|
mp_cmd_t *cmdt = mp_input_parse_cmd(self.vout->input_ctx, bstr0(cmd), "");
|
|
|
|
mp_input_queue_cmd(self.vout->input_ctx, cmdt);
|
|
|
|
ta_free(cmd);
|
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
- (void)performAsyncResize:(NSSize)size {
|
|
|
|
vo_cocoa_resize_redraw(self.vout, size.width, size.height);
|
2013-07-21 08:33:18 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
- (BOOL)isInFullScreenMode {
|
|
|
|
return self.vout->opts->fullscreen;
|
2011-10-15 16:44:00 +00:00
|
|
|
}
|
2013-06-01 20:52:21 +00:00
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
- (NSSize)videoSize {
|
|
|
|
return (NSSize) {
|
|
|
|
.width = self.vout->cocoa->aspdat.prew,
|
|
|
|
.height = self.vout->cocoa->aspdat.preh,
|
|
|
|
};
|
2013-06-01 20:52:21 +00:00
|
|
|
}
|
2013-07-21 08:33:18 +00:00
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
- (NSScreen *)fsScreen {
|
|
|
|
struct vo_cocoa_state *s = self.vout->cocoa;
|
|
|
|
return s->fs_screen;
|
2013-07-21 08:33:18 +00:00
|
|
|
}
|
|
|
|
@end
|