1
mirror of https://github.com/mpv-player/mpv synced 2024-11-14 22:48:35 +01:00
mpv/video/out/cocoa_common.m

1034 lines
30 KiB
Mathematica
Raw Normal View History

2012-03-04 22:04:07 +01:00
/*
* Cocoa OpenGL Backend
*
* This file is part of mpv.
2012-03-04 22:04:07 +01:00
*
* mpv is free software; you can redistribute it and/or modify
2012-03-04 22:04:07 +01: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.
*
* mpv is distributed in the hope that it will be useful,
2012-03-04 22:04:07 +01: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
* with mpv. If not, see <http://www.gnu.org/licenses/>.
2012-03-04 22:04:07 +01:00
*/
#import <Cocoa/Cocoa.h>
#import <IOKit/pwr_mgt/IOPMLib.h>
#import <IOKit/IOKitLib.h>
#import <AppKit/AppKit.h>
#include <mach/mach.h>
#import "cocoa_common.h"
#import "video/out/cocoa/window.h"
#import "video/out/cocoa/events_view.h"
#import "video/out/cocoa/video_view.h"
#import "video/out/cocoa/mpvadapter.h"
#include "osdep/threads.h"
#include "osdep/atomic.h"
#include "osdep/macosx_compat.h"
#include "osdep/macosx_events_objc.h"
#include "config.h"
#include "osdep/timer.h"
#include "osdep/macosx_application.h"
#include "osdep/macosx_application_objc.h"
#include "options/options.h"
#include "video/out/vo.h"
#include "win_state.h"
#include "input/input.h"
#include "mpv_talloc.h"
#include "common/msg.h"
static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now,
const CVTimeStamp* outputTime, CVOptionFlags flagsIn,
CVOptionFlags* flagsOut, void* displayLinkContext);
static int vo_cocoa_fullscreen(struct vo *vo);
static void cocoa_add_screen_reconfiguration_observer(struct vo *vo);
static void cocoa_rm_screen_reconfiguration_observer(struct vo *vo);
struct vo_cocoa_state {
// --- The following members can be accessed only by the main thread (i.e.
// where Cocoa runs), or if the main thread is fully blocked.
NSWindow *window;
NSView *view;
MpvVideoView *video;
2015-03-08 15:19:17 +01:00
MpvCocoaAdapter *adapter;
CGLContextObj cgl_ctx;
NSOpenGLContext *nsgl_ctx;
NSScreen *current_screen;
NSInteger window_level;
int fullscreen;
bool embedded; // wether we are embedding in another GUI
IOPMAssertionID power_mgmt_assertion;
io_connect_t light_sensor;
uint64_t last_lmuvalue;
int last_lux;
IONotificationPortRef light_sensor_io_port;
2013-08-01 08:23:53 +02:00
struct mp_log *log;
uint32_t old_dwidth;
uint32_t old_dheight;
CVDisplayLinkRef link;
pthread_mutex_t sync_lock;
pthread_cond_t sync_wakeup;
uint64_t sync_counter;
pthread_mutex_t lock;
pthread_cond_t wakeup;
// --- The following members are protected by the lock.
// If the VO and main threads are both blocked, locking is optional
// for members accessed only by VO and main thread.
int pending_events;
int vo_dwidth; // current or soon-to-be VO size
int vo_dheight;
bool vo_ready; // the VO is in a state in which it can
// render frames
int frame_w, frame_h; // dimensions of the frame rendered
NSCursor *blankCursor;
char *window_title;
};
static void run_on_main_thread(struct vo *vo, void(^block)(void))
{
dispatch_sync(dispatch_get_main_queue(), block);
}
static NSRect calculate_window_geometry(struct vo *vo, NSRect rect)
{
struct vo_cocoa_state *s = vo->cocoa;
struct mp_vo_opts *opts = vo->opts;
NSRect screenFrame = [s->current_screen frame];
rect.origin.y = screenFrame.size.height - (rect.origin.y + rect.size.height);
if(!opts->hidpi_window_scale) {
NSRect oldRect = rect;
rect = [s->current_screen convertRectFromBacking:rect];
CGFloat x_per = screenFrame.size.width - oldRect.size.width;
CGFloat y_per = screenFrame.size.height - oldRect.size.height;
if (x_per > 0) x_per = oldRect.origin.x/x_per;
if (y_per > 0) y_per = oldRect.origin.y/y_per;
rect.origin.x = (screenFrame.size.width - rect.size.width)*x_per;
rect.origin.y = (screenFrame.size.height - rect.size.height)*y_per;
}
return rect;
}
static void queue_new_video_size(struct vo *vo, int w, int h)
{
struct vo_cocoa_state *s = vo->cocoa;
struct mp_vo_opts *opts = vo->opts;
id<MpvWindowUpdate> win = (id<MpvWindowUpdate>) s->window;
NSRect r = calculate_window_geometry(vo, NSMakeRect(0, 0, w, h));
[win queueNewVideoSize:NSMakeSize(r.size.width, r.size.height)];
}
static void flag_events(struct vo *vo, int events)
{
struct vo_cocoa_state *s = vo->cocoa;
pthread_mutex_lock(&s->lock);
s->pending_events |= events;
pthread_mutex_unlock(&s->lock);
if (events)
vo_wakeup(vo);
}
static void enable_power_management(struct vo_cocoa_state *s)
{
if (!s->power_mgmt_assertion) return;
IOPMAssertionRelease(s->power_mgmt_assertion);
s->power_mgmt_assertion = kIOPMNullAssertionID;
}
static void disable_power_management(struct vo_cocoa_state *s)
{
if (s->power_mgmt_assertion) return;
IOPMAssertionCreateWithName(
kIOPMAssertionTypePreventUserIdleDisplaySleep,
kIOPMAssertionLevelOn,
CFSTR("io.mpv.video_playing_back"),
&s->power_mgmt_assertion);
}
static const char macosx_icon[] =
#include "osdep/macosx_icon.inc"
;
static void set_application_icon(NSApplication *app)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSBundle *bundle = [NSBundle mainBundle];
if ([bundle pathForResource:@"icon" ofType:@"icns"])
return;
NSData *icon_data = [NSData dataWithBytesNoCopy:(void *)macosx_icon
length:sizeof(macosx_icon)
freeWhenDone:NO];
NSImage *icon = [[NSImage alloc] initWithData:icon_data];
[app setApplicationIconImage:icon];
[icon release];
[pool release];
}
static int lmuvalue_to_lux(uint64_t v)
{
// the polinomial approximation for apple lmu value -> lux was empirically
// derived by firefox developers (Apple provides no documentation).
// https://bugzilla.mozilla.org/show_bug.cgi?id=793728
double power_c4 = 1/pow((double)10,27);
double power_c3 = 1/pow((double)10,19);
double power_c2 = 1/pow((double)10,12);
double power_c1 = 1/pow((double)10,5);
double term4 = -3.0 * power_c4 * pow(v,4);
double term3 = 2.6 * power_c3 * pow(v,3);
double term2 = -3.4 * power_c2 * pow(v,2);
double term1 = 3.9 * power_c1 * v;
int lux = ceil(term4 + term3 + term2 + term1 - 0.19);
return lux > 0 ? lux : 0;
}
static void light_sensor_cb(void *ctx, io_service_t srv, natural_t mtype, void *msg)
{
struct vo *vo = ctx;
struct vo_cocoa_state *s = vo->cocoa;
uint32_t outputs = 2;
uint64_t values[outputs];
kern_return_t kr = IOConnectCallMethod(
s->light_sensor, 0, NULL, 0, NULL, 0, values, &outputs, nil, 0);
if (kr == KERN_SUCCESS) {
uint64_t mean = (values[0] + values[1]) / 2;
if (s->last_lmuvalue != mean) {
s->last_lmuvalue = mean;
s->last_lux = lmuvalue_to_lux(s->last_lmuvalue);
flag_events(vo, VO_EVENT_AMBIENT_LIGHTING_CHANGED);
}
}
}
static void cocoa_init_light_sensor(struct vo *vo)
{
run_on_main_thread(vo, ^{
struct vo_cocoa_state *s = vo->cocoa;
io_service_t srv = IOServiceGetMatchingService(
kIOMasterPortDefault, IOServiceMatching("AppleLMUController"));
if (srv == IO_OBJECT_NULL) {
MP_VERBOSE(vo, "can't find an ambient light sensor\n");
return;
}
// subscribe to notifications from the light sensor driver
s->light_sensor_io_port = IONotificationPortCreate(kIOMasterPortDefault);
IONotificationPortSetDispatchQueue(
s->light_sensor_io_port, dispatch_get_main_queue());
io_object_t n;
IOServiceAddInterestNotification(
s->light_sensor_io_port, srv, kIOGeneralInterest, light_sensor_cb,
vo, &n);
kern_return_t kr = IOServiceOpen(srv, mach_task_self(), 0,
&s->light_sensor);
IOObjectRelease(srv);
if (kr != KERN_SUCCESS) {
MP_WARN(vo, "can't start ambient light sensor connection\n");
return;
}
light_sensor_cb(vo, 0, 0, NULL);
});
}
static void cocoa_uninit_light_sensor(struct vo_cocoa_state *s)
{
if (s->light_sensor_io_port) {
IONotificationPortDestroy(s->light_sensor_io_port);
IOObjectRelease(s->light_sensor);
}
}
static NSScreen *get_screen_by_id(struct vo *vo, int screen_id)
{
struct vo_cocoa_state *s = vo->cocoa;
NSArray *screens = [NSScreen screens];
int n_of_displays = [screens count];
if (screen_id >= n_of_displays) {
MP_INFO(s, "Screen ID %d does not exist, falling back to main "
"device\n", screen_id);
return nil;
} else if (screen_id < 0) {
return nil;
}
return [screens objectAtIndex:(screen_id)];
}
static void vo_cocoa_update_screen_info(struct vo *vo)
{
struct vo_cocoa_state *s = vo->cocoa;
struct mp_vo_opts *opts = vo->opts;
if (s->embedded)
return;
if (s->current_screen && s->window) {
s->current_screen = [s->window screen];
} else if (!s->current_screen) {
s->current_screen = get_screen_by_id(vo, opts->screen_id);
if (!s->current_screen)
s->current_screen = [NSScreen mainScreen];
}
}
static void vo_cocoa_init_displaylink(struct vo *vo)
{
struct vo_cocoa_state *s = vo->cocoa;
NSDictionary* sinfo = [s->current_screen deviceDescription];
NSNumber* sid = [sinfo objectForKey:@"NSScreenNumber"];
CGDirectDisplayID did = [sid longValue];
CVDisplayLinkCreateWithCGDisplay(did, &s->link);
CVDisplayLinkSetOutputCallback(s->link, &displayLinkCallback, vo);
CVDisplayLinkStart(s->link);
}
static void vo_cocoa_uninit_displaylink(struct vo_cocoa_state *s)
{
if (CVDisplayLinkIsRunning(s->link))
CVDisplayLinkStop(s->link);
CVDisplayLinkRelease(s->link);
}
void vo_cocoa_init(struct vo *vo)
{
struct vo_cocoa_state *s = talloc_zero(NULL, struct vo_cocoa_state);
*s = (struct vo_cocoa_state){
.power_mgmt_assertion = kIOPMNullAssertionID,
.log = mp_log_new(s, vo->log, "cocoa"),
.embedded = vo->opts->WinID >= 0,
.fullscreen = 0,
};
if (!s->embedded) {
NSImage* blankImage = [[NSImage alloc] initWithSize:NSMakeSize(1, 1)];
s->blankCursor = [[NSCursor alloc] initWithImage:blankImage hotSpot:NSZeroPoint];
[blankImage release];
}
pthread_mutex_init(&s->lock, NULL);
pthread_cond_init(&s->wakeup, NULL);
pthread_mutex_init(&s->sync_lock, NULL);
pthread_cond_init(&s->sync_wakeup, NULL);
vo->cocoa = s;
vo_cocoa_update_screen_info(vo);
vo_cocoa_init_displaylink(vo);
2015-03-07 09:11:47 +01:00
cocoa_init_light_sensor(vo);
cocoa_add_screen_reconfiguration_observer(vo);
if (!s->embedded) {
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
set_application_icon(NSApp);
}
}
static int vo_cocoa_set_cursor_visibility(struct vo *vo, bool *visible)
{
struct vo_cocoa_state *s = vo->cocoa;
if (s->embedded)
return VO_NOTIMPL;
MpvEventsView *v = (MpvEventsView *) s->view;
if (*visible) {
[[NSCursor arrowCursor] set];
} else if ([v canHideCursor] && s->blankCursor) {
[s->blankCursor set];
} else {
*visible = true;
}
return VO_TRUE;
}
void vo_cocoa_uninit(struct vo *vo)
{
struct vo_cocoa_state *s = vo->cocoa;
pthread_mutex_lock(&s->lock);
s->vo_ready = false;
pthread_cond_signal(&s->wakeup);
pthread_mutex_unlock(&s->lock);
// close window beforehand to prevent undefined behavior when in fullscreen
// that resets the desktop to space 1
run_on_main_thread(vo, ^{
// if using --wid + libmpv there's no window to release
if (s->window) {
[s->window setDelegate:nil];
[s->window close];
}
});
run_on_main_thread(vo, ^{
enable_power_management(s);
vo_cocoa_uninit_displaylink(s);
pthread_mutex_lock(&s->sync_lock);
pthread_cond_signal(&s->sync_wakeup);
pthread_mutex_unlock(&s->sync_lock);
cocoa_uninit_light_sensor(s);
cocoa_rm_screen_reconfiguration_observer(vo);
[s->nsgl_ctx release];
CGLReleaseContext(s->cgl_ctx);
// needed to stop resize events triggered by the event's view -clear
// causing many uses after free
[s->video removeFromSuperview];
[s->view removeFromSuperview];
[s->view release];
if (!s->embedded)
[s->blankCursor release];
pthread_cond_destroy(&s->sync_wakeup);
pthread_mutex_destroy(&s->sync_lock);
pthread_cond_destroy(&s->wakeup);
pthread_mutex_destroy(&s->lock);
talloc_free(s);
});
}
cocoa: fix displaylink refresh rate retrieval we are dealing with several problems here, which weren't apparent because we always initialised a new displaylink for the display refresh rate retrieval, previously to commit 449eb20 and bug 9490b62. just changing the display with CVDisplayLinkSetCurrentCGDisplay can cause inconsistent refresh rates and discontinuity in timestamps. this can either lead to bogus values for the Actual display refresh rate or retrieving the refresh rate of the previous display if we immediately try to get a new value. since the Actual refresh rate is computed i assume that it at least needs one refresh period to actual return something useful. furthermore when changing the screen and updating the displaylink, it seems that the retrieved refresh rates for the screen mpv wasn't opened on are being estimated in a sub-optimal way. as an example, when moving my window to my second screen the Actual refresh rate was always a constant 60Hz, even though it is supposed to fluctuate a little bit. though if mpv was started on the secondary screen the Actual refresh rate fluctuated around 59.94Hz like expected. in that case my primary screen always reported a constant 60Hz instead. for the first problem we moved the actual retrieval of the refresh rate to the very last moment when mpv actual requests a new value and not when the refresh rate changed. we only update the displaylink itself when a possible refresh rate change is detected. this gives the displaylink some time to calculate the new refresh rate. for the second problem, instead of setting the new display we completely uninitialise the old dislaylink and create a new one for the new screen. this gives us properly estimated refresh rates. additionally we also optimised the display refresh rate fallback heuristic. it will never be 0 anymore and we prevent it from returning bogus values with a simple threshold for the difference of the Actual and Nominal refresh rate.
2017-01-27 20:41:40 +01:00
static void vo_cocoa_update_displaylink(struct vo *vo)
{
struct vo_cocoa_state *s = vo->cocoa;
cocoa: fix displaylink refresh rate retrieval we are dealing with several problems here, which weren't apparent because we always initialised a new displaylink for the display refresh rate retrieval, previously to commit 449eb20 and bug 9490b62. just changing the display with CVDisplayLinkSetCurrentCGDisplay can cause inconsistent refresh rates and discontinuity in timestamps. this can either lead to bogus values for the Actual display refresh rate or retrieving the refresh rate of the previous display if we immediately try to get a new value. since the Actual refresh rate is computed i assume that it at least needs one refresh period to actual return something useful. furthermore when changing the screen and updating the displaylink, it seems that the retrieved refresh rates for the screen mpv wasn't opened on are being estimated in a sub-optimal way. as an example, when moving my window to my second screen the Actual refresh rate was always a constant 60Hz, even though it is supposed to fluctuate a little bit. though if mpv was started on the secondary screen the Actual refresh rate fluctuated around 59.94Hz like expected. in that case my primary screen always reported a constant 60Hz instead. for the first problem we moved the actual retrieval of the refresh rate to the very last moment when mpv actual requests a new value and not when the refresh rate changed. we only update the displaylink itself when a possible refresh rate change is detected. this gives the displaylink some time to calculate the new refresh rate. for the second problem, instead of setting the new display we completely uninitialise the old dislaylink and create a new one for the new screen. this gives us properly estimated refresh rates. additionally we also optimised the display refresh rate fallback heuristic. it will never be 0 anymore and we prevent it from returning bogus values with a simple threshold for the difference of the Actual and Nominal refresh rate.
2017-01-27 20:41:40 +01:00
vo_cocoa_uninit_displaylink(s);
vo_cocoa_init_displaylink(vo);
cocoa: fix displaylink refresh rate retrieval we are dealing with several problems here, which weren't apparent because we always initialised a new displaylink for the display refresh rate retrieval, previously to commit 449eb20 and bug 9490b62. just changing the display with CVDisplayLinkSetCurrentCGDisplay can cause inconsistent refresh rates and discontinuity in timestamps. this can either lead to bogus values for the Actual display refresh rate or retrieving the refresh rate of the previous display if we immediately try to get a new value. since the Actual refresh rate is computed i assume that it at least needs one refresh period to actual return something useful. furthermore when changing the screen and updating the displaylink, it seems that the retrieved refresh rates for the screen mpv wasn't opened on are being estimated in a sub-optimal way. as an example, when moving my window to my second screen the Actual refresh rate was always a constant 60Hz, even though it is supposed to fluctuate a little bit. though if mpv was started on the secondary screen the Actual refresh rate fluctuated around 59.94Hz like expected. in that case my primary screen always reported a constant 60Hz instead. for the first problem we moved the actual retrieval of the refresh rate to the very last moment when mpv actual requests a new value and not when the refresh rate changed. we only update the displaylink itself when a possible refresh rate change is detected. this gives the displaylink some time to calculate the new refresh rate. for the second problem, instead of setting the new display we completely uninitialise the old dislaylink and create a new one for the new screen. this gives us properly estimated refresh rates. additionally we also optimised the display refresh rate fallback heuristic. it will never be 0 anymore and we prevent it from returning bogus values with a simple threshold for the difference of the Actual and Nominal refresh rate.
2017-01-27 20:41:40 +01:00
flag_events(vo, VO_EVENT_WIN_STATE);
}
cocoa: fix displaylink refresh rate retrieval we are dealing with several problems here, which weren't apparent because we always initialised a new displaylink for the display refresh rate retrieval, previously to commit 449eb20 and bug 9490b62. just changing the display with CVDisplayLinkSetCurrentCGDisplay can cause inconsistent refresh rates and discontinuity in timestamps. this can either lead to bogus values for the Actual display refresh rate or retrieving the refresh rate of the previous display if we immediately try to get a new value. since the Actual refresh rate is computed i assume that it at least needs one refresh period to actual return something useful. furthermore when changing the screen and updating the displaylink, it seems that the retrieved refresh rates for the screen mpv wasn't opened on are being estimated in a sub-optimal way. as an example, when moving my window to my second screen the Actual refresh rate was always a constant 60Hz, even though it is supposed to fluctuate a little bit. though if mpv was started on the secondary screen the Actual refresh rate fluctuated around 59.94Hz like expected. in that case my primary screen always reported a constant 60Hz instead. for the first problem we moved the actual retrieval of the refresh rate to the very last moment when mpv actual requests a new value and not when the refresh rate changed. we only update the displaylink itself when a possible refresh rate change is detected. this gives the displaylink some time to calculate the new refresh rate. for the second problem, instead of setting the new display we completely uninitialise the old dislaylink and create a new one for the new screen. this gives us properly estimated refresh rates. additionally we also optimised the display refresh rate fallback heuristic. it will never be 0 anymore and we prevent it from returning bogus values with a simple threshold for the difference of the Actual and Nominal refresh rate.
2017-01-27 20:41:40 +01:00
static double vo_cocoa_update_screen_fps(struct vo *vo)
{
struct vo_cocoa_state *s = vo->cocoa;
double actual_fps = CVDisplayLinkGetActualOutputVideoRefreshPeriod(s->link);
const CVTime t = CVDisplayLinkGetNominalOutputVideoRefreshPeriod(s->link);
if (!(t.flags & kCVTimeIsIndefinite)) {
double nominal_fps = (t.timeScale / (double) t.timeValue);
if (actual_fps > 0)
actual_fps = 1/actual_fps;
if (fabs(actual_fps - nominal_fps) > 0.1) {
MP_VERBOSE(vo, "Falling back to nominal display "
"refresh rate: %fHz\n", nominal_fps);
return nominal_fps;
} else {
return actual_fps;
}
}
cocoa: fix displaylink refresh rate retrieval we are dealing with several problems here, which weren't apparent because we always initialised a new displaylink for the display refresh rate retrieval, previously to commit 449eb20 and bug 9490b62. just changing the display with CVDisplayLinkSetCurrentCGDisplay can cause inconsistent refresh rates and discontinuity in timestamps. this can either lead to bogus values for the Actual display refresh rate or retrieving the refresh rate of the previous display if we immediately try to get a new value. since the Actual refresh rate is computed i assume that it at least needs one refresh period to actual return something useful. furthermore when changing the screen and updating the displaylink, it seems that the retrieved refresh rates for the screen mpv wasn't opened on are being estimated in a sub-optimal way. as an example, when moving my window to my second screen the Actual refresh rate was always a constant 60Hz, even though it is supposed to fluctuate a little bit. though if mpv was started on the secondary screen the Actual refresh rate fluctuated around 59.94Hz like expected. in that case my primary screen always reported a constant 60Hz instead. for the first problem we moved the actual retrieval of the refresh rate to the very last moment when mpv actual requests a new value and not when the refresh rate changed. we only update the displaylink itself when a possible refresh rate change is detected. this gives the displaylink some time to calculate the new refresh rate. for the second problem, instead of setting the new display we completely uninitialise the old dislaylink and create a new one for the new screen. this gives us properly estimated refresh rates. additionally we also optimised the display refresh rate fallback heuristic. it will never be 0 anymore and we prevent it from returning bogus values with a simple threshold for the difference of the Actual and Nominal refresh rate.
2017-01-27 20:41:40 +01:00
MP_WARN(vo, "Falling back to standard display refresh rate: 60Hz\n");
return 60.0;
}
static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now,
const CVTimeStamp* outputTime, CVOptionFlags flagsIn,
CVOptionFlags* flagsOut, void* displayLinkContext)
{
struct vo *vo = displayLinkContext;
struct vo_cocoa_state *s = vo->cocoa;
pthread_mutex_lock(&s->sync_lock);
s->sync_counter += 1;
pthread_cond_signal(&s->sync_wakeup);
pthread_mutex_unlock(&s->sync_lock);
return kCVReturnSuccess;
}
static void vo_set_level(struct vo *vo, int ontop)
{
struct vo_cocoa_state *s = vo->cocoa;
if (ontop) {
// +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;
} else {
s->window_level = NSNormalWindowLevel;
}
[s->window setLevel:s->window_level];
NSWindowCollectionBehavior behavior = [s->window collectionBehavior] &
~NSWindowCollectionBehaviorTransient;
[s->window setCollectionBehavior:behavior|NSWindowCollectionBehaviorManaged];
}
static int vo_cocoa_ontop(struct vo *vo)
{
struct vo_cocoa_state *s = vo->cocoa;
if (s->embedded)
return VO_NOTIMPL;
struct mp_vo_opts *opts = vo->opts;
vo_set_level(vo, opts->ontop);
return VO_TRUE;
}
static MpvVideoWindow *create_window(NSRect rect, NSScreen *s, bool border,
MpvCocoaAdapter *adapter)
{
int window_mask = 0;
if (border) {
window_mask = NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|
NSWindowStyleMaskMiniaturizable|NSWindowStyleMaskResizable;
} else {
window_mask = NSWindowStyleMaskBorderless|NSWindowStyleMaskResizable;
}
MpvVideoWindow *w =
[[MpvVideoWindow alloc] initWithContentRect:rect
styleMask:window_mask
backing:NSBackingStoreBuffered
defer:NO
screen:s];
w.adapter = adapter;
[w setDelegate: w];
return w;
}
static void create_ui(struct vo *vo, struct mp_rect *win, int geo_flags)
{
struct vo_cocoa_state *s = vo->cocoa;
struct mp_vo_opts *opts = vo->opts;
MpvCocoaAdapter *adapter = [[MpvCocoaAdapter alloc] init];
adapter.vout = vo;
NSView *parent;
if (s->embedded) {
parent = (NSView *) (intptr_t) opts->WinID;
} else {
NSRect wr = calculate_window_geometry(vo,
NSMakeRect(win->x0, win->y0, win->x1 - win->x0, win->y1 - win->y0));
s->window = create_window(wr, s->current_screen, opts->border, adapter);
parent = [s->window contentView];
}
MpvEventsView *view = [[MpvEventsView alloc] initWithFrame:[parent bounds]];
view.adapter = adapter;
s->view = view;
[parent addSubview:s->view];
// update the cursor position now that the view has been added.
[view signalMousePosition];
2015-03-08 15:19:17 +01:00
s->adapter = adapter;
OSX: use native Cocoa's event loop Schedule mpv's playloop as a high frequency timer inside the main Cocoa event loop. This has the benefit to allow accessing menus as well as resizing the window without the playback being blocked and allows to remove countless hacks from the code that involved manually pumping the event loop as well simulating manually some of the Cocoa default behaviours. A huge improvement consists in removing NSApplicationLoad. This is a C function defined in the Cocoa header and implements a minimal OSX application under ther hood so that you can use the Cocoa GUI toolkit from C/C++ without having to respect the Cocoa standards in terms of application initialization. This was bad because the behaviour implemented by NSApplicationLoad was hard to customize and had several gotchas especially in the menu department. mpv was changed to be just a nib-less application. All the Cocoa part is still generated in code but the event handling is now not dissimilar to what is present in a stock Mac application. As a part of reviewing the initialization process, I also removed all of `osdep/macosx_finder_args`. The useful parts of the code were moved to `osdep/macosx_appication` which has the broaded responsibility of managing the full lifecycle of the Cocoa application. By consequence the `--enable-macosx-finder` configure switch was killed as well, as this feature is always enabled. Another change the users will notice is that when using a bundle the `--quiet` option will be inserted much earlier in the initializaion process. This results in mpv not spamming mpv.log anymore with all the initialization outputs.
2013-02-23 18:28:22 +01: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:));
s->video = [[MpvVideoView alloc] initWithFrame:[s->view bounds]];
[s->video setWantsBestResolutionOpenGLSurface:YES];
[s->view addSubview:s->video];
[s->nsgl_ctx setView:s->video];
[s->video release];
s->video.adapter = adapter;
2014-10-27 19:18:29 +01:00
[adapter release];
if (!s->embedded) {
[s->window setRestorable:NO];
[s->window makeMainWindow];
[s->window makeKeyAndOrderFront:nil];
[NSApp activateIgnoringOtherApps:YES];
}
}
static int cocoa_set_window_title(struct vo *vo)
{
struct vo_cocoa_state *s = vo->cocoa;
if (s->embedded)
return VO_NOTIMPL;
void *talloc_ctx = talloc_new(NULL);
struct bstr btitle =
bstr_sanitize_utf8_latin1(talloc_ctx, bstr0(s->window_title));
if (btitle.start) {
NSString *nstitle = [NSString stringWithUTF8String:btitle.start];
if (nstitle) {
[s->window setTitle: nstitle];
[s->window displayIfNeeded];
}
}
talloc_free(talloc_ctx);
return VO_TRUE;
}
2016-12-15 22:12:46 +01:00
static int vo_cocoa_window_border(struct vo *vo)
{
struct vo_cocoa_state *s = vo->cocoa;
if (s->embedded)
return VO_NOTIMPL;
struct mp_vo_opts *opts = vo->opts;
id<MpvWindowUpdate> win = (id<MpvWindowUpdate>) s->window;
[win updateBorder:opts->border];
2016-12-15 22:12:46 +01:00
if (opts->border)
cocoa_set_window_title(vo);
return VO_TRUE;
}
static void cocoa_screen_reconfiguration_observer(
CGDirectDisplayID display, CGDisplayChangeSummaryFlags flags, void *ctx)
{
if (flags & kCGDisplaySetModeFlag) {
struct vo *vo = ctx;
MP_WARN(vo, "detected display mode change, updating screen info\n");
cocoa: fix displaylink refresh rate retrieval we are dealing with several problems here, which weren't apparent because we always initialised a new displaylink for the display refresh rate retrieval, previously to commit 449eb20 and bug 9490b62. just changing the display with CVDisplayLinkSetCurrentCGDisplay can cause inconsistent refresh rates and discontinuity in timestamps. this can either lead to bogus values for the Actual display refresh rate or retrieving the refresh rate of the previous display if we immediately try to get a new value. since the Actual refresh rate is computed i assume that it at least needs one refresh period to actual return something useful. furthermore when changing the screen and updating the displaylink, it seems that the retrieved refresh rates for the screen mpv wasn't opened on are being estimated in a sub-optimal way. as an example, when moving my window to my second screen the Actual refresh rate was always a constant 60Hz, even though it is supposed to fluctuate a little bit. though if mpv was started on the secondary screen the Actual refresh rate fluctuated around 59.94Hz like expected. in that case my primary screen always reported a constant 60Hz instead. for the first problem we moved the actual retrieval of the refresh rate to the very last moment when mpv actual requests a new value and not when the refresh rate changed. we only update the displaylink itself when a possible refresh rate change is detected. this gives the displaylink some time to calculate the new refresh rate. for the second problem, instead of setting the new display we completely uninitialise the old dislaylink and create a new one for the new screen. this gives us properly estimated refresh rates. additionally we also optimised the display refresh rate fallback heuristic. it will never be 0 anymore and we prevent it from returning bogus values with a simple threshold for the difference of the Actual and Nominal refresh rate.
2017-01-27 20:41:40 +01:00
vo_cocoa_update_displaylink(vo);
}
}
static void cocoa_add_screen_reconfiguration_observer(struct vo *vo)
{
CGDisplayRegisterReconfigurationCallback(
cocoa_screen_reconfiguration_observer, vo);
}
static void cocoa_rm_screen_reconfiguration_observer(struct vo *vo)
{
CGDisplayRemoveReconfigurationCallback(
cocoa_screen_reconfiguration_observer, vo);
}
void vo_cocoa_set_opengl_ctx(struct vo *vo, CGLContextObj ctx)
{
struct vo_cocoa_state *s = vo->cocoa;
run_on_main_thread(vo, ^{
s->cgl_ctx = CGLRetainContext(ctx);
s->nsgl_ctx = [[NSOpenGLContext alloc] initWithCGLContextObj:s->cgl_ctx];
});
}
int vo_cocoa_config_window(struct vo *vo)
{
struct vo_cocoa_state *s = vo->cocoa;
struct mp_vo_opts *opts = vo->opts;
run_on_main_thread(vo, ^{
NSRect r = [s->current_screen frame];
struct mp_rect screenrc = {0, 0, r.size.width, r.size.height};
struct vo_win_geometry geo;
vo_calc_window_geometry(vo, &screenrc, &geo);
vo_apply_window_geometry(vo, &geo);
uint32_t width = vo->dwidth;
uint32_t height = vo->dheight;
bool reset_size = s->old_dwidth != width || s->old_dheight != height;
s->old_dwidth = width;
s->old_dheight = height;
if (!s->view) {
create_ui(vo, &geo.win, geo.flags);
}
2014-10-05 22:38:26 +02:00
if (!s->embedded && s->window) {
if (reset_size)
queue_new_video_size(vo, width, height);
if (opts->fullscreen && !s->fullscreen)
vo_cocoa_fullscreen(vo);
cocoa_set_window_title(vo);
vo_set_level(vo, opts->ontop);
GLint o;
if (!CGLGetParameter(s->cgl_ctx, kCGLCPSurfaceOpacity, &o) && !o) {
[s->window setOpaque:NO];
[s->window setBackgroundColor:[NSColor clearColor]];
}
}
s->vo_ready = true;
// Use the actual size of the new window
NSRect frame = [s->video frameInPixels];
vo->dwidth = s->vo_dwidth = frame.size.width;
vo->dheight = s->vo_dheight = frame.size.height;
[s->nsgl_ctx update];
});
return 0;
}
// Trigger a VO resize - called from the main thread. This is done async,
// because the VO must resize and redraw while vo_cocoa_resize_redraw() is
// blocking.
static void resize_event(struct vo *vo)
{
struct vo_cocoa_state *s = vo->cocoa;
NSRect frame = [s->video frameInPixels];
pthread_mutex_lock(&s->lock);
s->vo_dwidth = frame.size.width;
s->vo_dheight = frame.size.height;
s->pending_events |= VO_EVENT_RESIZE | VO_EVENT_EXPOSE;
// Live-resizing: make sure at least one frame will be drawn
s->frame_w = s->frame_h = 0;
pthread_mutex_unlock(&s->lock);
[s->nsgl_ctx update];
vo_wakeup(vo);
}
static void vo_cocoa_resize_redraw(struct vo *vo, int width, int height)
{
struct vo_cocoa_state *s = vo->cocoa;
resize_event(vo);
pthread_mutex_lock(&s->lock);
// Wait until a new frame with the new size was rendered. For some reason,
// Cocoa requires this to be done before drawRect() returns.
struct timespec e = mp_time_us_to_timespec(mp_add_timeout(mp_time_us(), 0.1));
while (s->frame_w != width && s->frame_h != height && s->vo_ready) {
if (pthread_cond_timedwait(&s->wakeup, &s->lock, &e))
break;
}
pthread_mutex_unlock(&s->lock);
}
void vo_cocoa_swap_buffers(struct vo *vo)
{
struct vo_cocoa_state *s = vo->cocoa;
// Don't swap a frame with wrong size
pthread_mutex_lock(&s->lock);
bool skip = s->pending_events & VO_EVENT_RESIZE;
pthread_mutex_unlock(&s->lock);
if (skip)
return;
pthread_mutex_lock(&s->sync_lock);
uint64_t old_counter = s->sync_counter;
while(old_counter == s->sync_counter) {
pthread_cond_wait(&s->sync_wakeup, &s->sync_lock);
}
pthread_mutex_unlock(&s->sync_lock);
pthread_mutex_lock(&s->lock);
s->frame_w = vo->dwidth;
s->frame_h = vo->dheight;
pthread_cond_signal(&s->wakeup);
pthread_mutex_unlock(&s->lock);
}
static int vo_cocoa_check_events(struct vo *vo)
{
struct vo_cocoa_state *s = vo->cocoa;
pthread_mutex_lock(&s->lock);
2014-10-05 19:31:21 +02:00
int events = s->pending_events;
2014-10-05 22:32:23 +02:00
s->pending_events = 0;
if (events & VO_EVENT_RESIZE) {
vo->dwidth = s->vo_dwidth;
vo->dheight = s->vo_dheight;
}
pthread_mutex_unlock(&s->lock);
2014-10-05 19:31:21 +02:00
return events;
}
static int vo_cocoa_fullscreen(struct vo *vo)
{
struct vo_cocoa_state *s = vo->cocoa;
if (s->embedded)
return VO_NOTIMPL;
[s->window toggleFullScreen:nil];
// for whatever reason sometimes cocoa doesn't create an up event on
// the fullscreen input key
cocoa_put_key(MP_INPUT_RELEASE_ALL);
return VO_TRUE;
}
static void vo_cocoa_control_get_icc_profile(struct vo *vo, void *arg)
{
struct vo_cocoa_state *s = vo->cocoa;
bstr *p = arg;
NSData *profile = [[s->current_screen colorSpace] ICCProfileData];
p->start = talloc_memdup(NULL, (void *)[profile bytes], [profile length]);
p->len = [profile length];
}
static int vo_cocoa_control_on_main_thread(struct vo *vo, int request, void *arg)
{
struct vo_cocoa_state *s = vo->cocoa;
switch (request) {
case VOCTRL_FULLSCREEN:
return vo_cocoa_fullscreen(vo);
case VOCTRL_GET_FULLSCREEN:
*(int *)arg = s->fullscreen;
return VO_TRUE;
case VOCTRL_ONTOP:
return vo_cocoa_ontop(vo);
2016-12-15 22:12:46 +01:00
case VOCTRL_BORDER:
return vo_cocoa_window_border(vo);
case VOCTRL_GET_UNFS_WINDOW_SIZE: {
int *sz = arg;
NSSize size = [s->view frame].size;
sz[0] = size.width;
sz[1] = size.height;
2013-11-22 08:41:34 +01:00
return VO_TRUE;
}
case VOCTRL_SET_UNFS_WINDOW_SIZE: {
int *sz = arg;
int w, h;
w = sz[0];
h = sz[1];
queue_new_video_size(vo, w, h);
2013-11-22 08:41:34 +01:00
return VO_TRUE;
}
case VOCTRL_GET_WIN_STATE: {
const bool minimized = [[s->view window] isMiniaturized];
*(int *)arg = minimized ? VO_WIN_STATE_MINIMIZED : 0;
return VO_TRUE;
}
case VOCTRL_SET_CURSOR_VISIBILITY:
return vo_cocoa_set_cursor_visibility(vo, arg);
case VOCTRL_UPDATE_WINDOW_TITLE: {
talloc_free(s->window_title);
s->window_title = talloc_strdup(s, (char *) arg);
return cocoa_set_window_title(vo);
}
case VOCTRL_RESTORE_SCREENSAVER:
enable_power_management(s);
return VO_TRUE;
case VOCTRL_KILL_SCREENSAVER:
disable_power_management(s);
return VO_TRUE;
case VOCTRL_GET_ICC_PROFILE:
vo_cocoa_control_get_icc_profile(vo, arg);
return VO_TRUE;
case VOCTRL_GET_DISPLAY_FPS:
cocoa: fix displaylink refresh rate retrieval we are dealing with several problems here, which weren't apparent because we always initialised a new displaylink for the display refresh rate retrieval, previously to commit 449eb20 and bug 9490b62. just changing the display with CVDisplayLinkSetCurrentCGDisplay can cause inconsistent refresh rates and discontinuity in timestamps. this can either lead to bogus values for the Actual display refresh rate or retrieving the refresh rate of the previous display if we immediately try to get a new value. since the Actual refresh rate is computed i assume that it at least needs one refresh period to actual return something useful. furthermore when changing the screen and updating the displaylink, it seems that the retrieved refresh rates for the screen mpv wasn't opened on are being estimated in a sub-optimal way. as an example, when moving my window to my second screen the Actual refresh rate was always a constant 60Hz, even though it is supposed to fluctuate a little bit. though if mpv was started on the secondary screen the Actual refresh rate fluctuated around 59.94Hz like expected. in that case my primary screen always reported a constant 60Hz instead. for the first problem we moved the actual retrieval of the refresh rate to the very last moment when mpv actual requests a new value and not when the refresh rate changed. we only update the displaylink itself when a possible refresh rate change is detected. this gives the displaylink some time to calculate the new refresh rate. for the second problem, instead of setting the new display we completely uninitialise the old dislaylink and create a new one for the new screen. this gives us properly estimated refresh rates. additionally we also optimised the display refresh rate fallback heuristic. it will never be 0 anymore and we prevent it from returning bogus values with a simple threshold for the difference of the Actual and Nominal refresh rate.
2017-01-27 20:41:40 +01:00
*(double *)arg = vo_cocoa_update_screen_fps(vo);
return VO_TRUE;
break;
case VOCTRL_GET_AMBIENT_LUX:
if (s->light_sensor != IO_OBJECT_NULL) {
*(int *)arg = s->last_lux;
return VO_TRUE;
}
break;
}
return VO_NOTIMPL;
}
static int vo_cocoa_control_async(struct vo *vo, int *events, int request, void *arg)
{
switch (request) {
case VOCTRL_CHECK_EVENTS:
*events |= vo_cocoa_check_events(vo);
return VO_TRUE;
}
return VO_NOTIMPL;
}
int vo_cocoa_control(struct vo *vo, int *events, int request, void *arg)
{
__block int r = vo_cocoa_control_async(vo, events, request, arg);
if (r == VO_NOTIMPL) {
run_on_main_thread(vo, ^{
r = vo_cocoa_control_on_main_thread(vo, request, arg);
});
}
return r;
}
@implementation MpvCocoaAdapter
@synthesize vout = _video_output;
2016-12-15 23:06:04 +01:00
- (void)performAsyncResize:(NSSize)size
{
struct vo_cocoa_state *s = self.vout->cocoa;
vo_cocoa_resize_redraw(self.vout, size.width, size.height);
}
2016-12-15 23:06:04 +01:00
- (BOOL)keyboardEnabled
{
return !!mp_input_vo_keyboard_enabled(self.vout->input_ctx);
}
2016-12-15 23:06:04 +01:00
- (BOOL)mouseEnabled
{
return !!mp_input_mouse_enabled(self.vout->input_ctx);
}
2016-12-15 23:06:04 +01:00
- (void)setNeedsResize
{
resize_event(self.vout);
}
- (void)recalcMovableByWindowBackground:(NSPoint)p
{
BOOL movable = NO;
if (!self.vout->cocoa->fullscreen) {
movable = !mp_input_test_dragging(self.vout->input_ctx, p.x, p.y);
}
[self.vout->cocoa->window setMovableByWindowBackground:movable];
}
2016-12-15 23:06:04 +01:00
- (void)signalMouseMovement:(NSPoint)point
{
mp_input_set_mouse_pos(self.vout->input_ctx, point.x, point.y);
[self recalcMovableByWindowBackground:point];
}
- (void)putKeyEvent:(NSEvent*)event
{
cocoa_put_key_event(event);
}
- (void)putKey:(int)mpkey withModifiers:(int)modifiers
{
cocoa_put_key_with_modifiers(mpkey, modifiers);
}
- (void)putAxis:(int)mpkey delta:(float)delta;
{
mp_input_put_axis(self.vout->input_ctx, mpkey, delta);
}
- (void)putCommand:(char*)cmd
{
char *cmd_ = ta_strdup(NULL, 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_);
}
- (BOOL)isInFullScreenMode
{
return self.vout->cocoa->fullscreen;
}
- (NSScreen *)getTargetScreen
{
struct vo_cocoa_state *s = self.vout->cocoa;
struct mp_vo_opts *opts = self.vout->opts;
int screen_id = s->fullscreen ? opts->screen_id : opts->fsscreen_id;
return get_screen_by_id(self.vout, screen_id);
}
- (void)handleFilesArray:(NSArray *)files
{
[[EventsResponder sharedInstance] handleFilesArray:files];
}
- (void)windowDidChangeScreen:(NSNotification *)notification
{
vo_cocoa_update_screen_info(self.vout);
cocoa: fix displaylink refresh rate retrieval we are dealing with several problems here, which weren't apparent because we always initialised a new displaylink for the display refresh rate retrieval, previously to commit 449eb20 and bug 9490b62. just changing the display with CVDisplayLinkSetCurrentCGDisplay can cause inconsistent refresh rates and discontinuity in timestamps. this can either lead to bogus values for the Actual display refresh rate or retrieving the refresh rate of the previous display if we immediately try to get a new value. since the Actual refresh rate is computed i assume that it at least needs one refresh period to actual return something useful. furthermore when changing the screen and updating the displaylink, it seems that the retrieved refresh rates for the screen mpv wasn't opened on are being estimated in a sub-optimal way. as an example, when moving my window to my second screen the Actual refresh rate was always a constant 60Hz, even though it is supposed to fluctuate a little bit. though if mpv was started on the secondary screen the Actual refresh rate fluctuated around 59.94Hz like expected. in that case my primary screen always reported a constant 60Hz instead. for the first problem we moved the actual retrieval of the refresh rate to the very last moment when mpv actual requests a new value and not when the refresh rate changed. we only update the displaylink itself when a possible refresh rate change is detected. this gives the displaylink some time to calculate the new refresh rate. for the second problem, instead of setting the new display we completely uninitialise the old dislaylink and create a new one for the new screen. this gives us properly estimated refresh rates. additionally we also optimised the display refresh rate fallback heuristic. it will never be 0 anymore and we prevent it from returning bogus values with a simple threshold for the difference of the Actual and Nominal refresh rate.
2017-01-27 20:41:40 +01:00
vo_cocoa_update_displaylink(self.vout);
}
- (void)windowDidEnterFullScreen:(NSNotification *)notification
{
struct vo_cocoa_state *s = self.vout->cocoa;
s->fullscreen = 1;
s->pending_events |= VO_EVENT_FULLSCREEN_STATE;
}
- (void)windowDidExitFullScreen:(NSNotification *)notification
{
struct vo_cocoa_state *s = self.vout->cocoa;
s->fullscreen = 0;
s->pending_events |= VO_EVENT_FULLSCREEN_STATE;
}
- (void)windowWillStartLiveResize:(NSNotification *)notification
{
// Make vo.c not do video timing, which would slow down resizing.
vo_event(self.vout, VO_EVENT_LIVE_RESIZING);
}
- (void)windowDidEndLiveResize:(NSNotification *)notification
{
vo_query_and_reset_events(self.vout, VO_EVENT_LIVE_RESIZING);
}
- (void)didChangeWindowedScreenProfile:(NSNotification *)notification
{
flag_events(self.vout, VO_EVENT_ICC_PROFILE_CHANGED);
}
- (void)didChangeMousePosition
{
struct vo_cocoa_state *s = self.vout->cocoa;
[(MpvEventsView *)s->view signalMousePosition];
}
2015-03-08 15:19:17 +01:00
- (void)windowDidResignKey:(NSNotification *)notification
{
[self didChangeMousePosition];
}
- (void)windowDidBecomeKey:(NSNotification *)notification
{
[self didChangeMousePosition];
}
- (void)windowDidMiniaturize:(NSNotification *)notification
{
flag_events(self.vout, VO_EVENT_WIN_STATE);
}
- (void)windowDidDeminiaturize:(NSNotification *)notification
{
flag_events(self.vout, VO_EVENT_WIN_STATE);
}
@end