2012-03-04 22:04:07 +01:00
|
|
|
/*
|
|
|
|
* Cocoa OpenGL Backend
|
|
|
|
*
|
2013-09-03 21:18:28 +02:00
|
|
|
* This file is part of mpv.
|
2012-03-04 22:04:07 +01:00
|
|
|
*
|
2017-06-24 16:08:17 +02:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2012-03-04 22:04:07 +01:00
|
|
|
*
|
2013-09-03 21:18:28 +02:00
|
|
|
* 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
|
2017-06-24 16:08:17 +02:00
|
|
|
* GNU Lesser General Public License for more details.
|
2012-03-04 22:04:07 +01:00
|
|
|
*
|
2017-06-24 16:08:17 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2012-03-04 22:04:07 +01:00
|
|
|
*/
|
|
|
|
|
2011-10-15 18:44:00 +02:00
|
|
|
#import <Cocoa/Cocoa.h>
|
2012-09-16 20:53:04 +02:00
|
|
|
#import <IOKit/pwr_mgt/IOPMLib.h>
|
2015-02-03 18:16:02 +01:00
|
|
|
#import <IOKit/IOKitLib.h>
|
2015-08-10 22:14:39 +02:00
|
|
|
#import <AppKit/AppKit.h>
|
2015-02-03 18:16:02 +01:00
|
|
|
#include <mach/mach.h>
|
2012-04-14 15:02:28 +02:00
|
|
|
|
2014-10-05 22:31:33 +02:00
|
|
|
#import "cocoa_common.h"
|
|
|
|
#import "video/out/cocoa/window.h"
|
|
|
|
#import "video/out/cocoa/events_view.h"
|
|
|
|
#import "video/out/cocoa/video_view.h"
|
2013-09-03 21:18:28 +02:00
|
|
|
#import "video/out/cocoa/mpvadapter.h"
|
|
|
|
|
2014-10-04 11:47:17 +02:00
|
|
|
#include "osdep/threads.h"
|
2016-09-07 11:26:25 +02:00
|
|
|
#include "osdep/atomic.h"
|
2013-10-05 14:39:27 +02:00
|
|
|
#include "osdep/macosx_compat.h"
|
2014-04-16 04:38:19 +02:00
|
|
|
#include "osdep/macosx_events_objc.h"
|
2011-10-15 18:44:00 +02:00
|
|
|
|
2012-07-21 10:37:13 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
2015-05-12 22:31:03 +02:00
|
|
|
#include "osdep/timer.h"
|
2015-05-02 18:09:01 +02:00
|
|
|
#include "osdep/macosx_application.h"
|
|
|
|
#include "osdep/macosx_application_objc.h"
|
2014-04-16 04:52:24 +02:00
|
|
|
|
2013-12-17 03:35:43 +01:00
|
|
|
#include "options/options.h"
|
2013-09-03 21:18:28 +02:00
|
|
|
#include "video/out/vo.h"
|
2014-05-06 22:00:17 +02:00
|
|
|
#include "win_state.h"
|
2011-10-15 18:44:00 +02:00
|
|
|
|
2013-12-17 03:35:43 +01:00
|
|
|
#include "input/input.h"
|
2016-01-11 19:03:40 +01:00
|
|
|
#include "mpv_talloc.h"
|
2011-10-15 18:44:00 +02:00
|
|
|
|
2013-12-17 03:35:43 +01:00
|
|
|
#include "common/msg.h"
|
2011-10-15 18:44:00 +02:00
|
|
|
|
2016-12-04 22:52:14 +01:00
|
|
|
static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now,
|
|
|
|
const CVTimeStamp* outputTime, CVOptionFlags flagsIn,
|
2016-06-16 19:28:14 +02:00
|
|
|
CVOptionFlags* flagsOut, void* displayLinkContext);
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
static int vo_cocoa_fullscreen(struct vo *vo);
|
2016-01-06 13:27:33 +01:00
|
|
|
static void cocoa_add_screen_reconfiguration_observer(struct vo *vo);
|
|
|
|
static void cocoa_rm_screen_reconfiguration_observer(struct vo *vo);
|
2013-07-21 10:33:18 +02:00
|
|
|
|
2011-10-15 18:44:00 +02:00
|
|
|
struct vo_cocoa_state {
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
// --- The following members can be accessed only by the main thread (i.e.
|
|
|
|
// where Cocoa runs), or if the main thread is fully blocked.
|
|
|
|
|
2014-10-05 14:28:33 +02:00
|
|
|
NSWindow *window;
|
2014-10-08 19:23:47 +02:00
|
|
|
NSView *view;
|
2014-10-05 22:31:33 +02:00
|
|
|
MpvVideoView *video;
|
2015-03-08 15:19:17 +01:00
|
|
|
MpvCocoaAdapter *adapter;
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
|
|
|
|
CGLContextObj cgl_ctx;
|
|
|
|
NSOpenGLContext *nsgl_ctx;
|
2011-10-15 18:44:00 +02:00
|
|
|
|
2013-05-29 00:14:34 +02:00
|
|
|
NSScreen *current_screen;
|
2017-02-13 18:37:51 +01:00
|
|
|
CGDirectDisplayID display_id;
|
2011-10-15 18:44:00 +02:00
|
|
|
|
2012-09-30 00:32:53 +02:00
|
|
|
NSInteger window_level;
|
2016-12-04 22:52:14 +01:00
|
|
|
int fullscreen;
|
2017-03-14 02:38:54 +01:00
|
|
|
NSRect unfs_window;
|
2011-12-07 12:42:04 +01:00
|
|
|
|
2017-02-10 15:33:01 +01:00
|
|
|
bool cursor_visibility;
|
|
|
|
bool cursor_visibility_wanted;
|
2017-02-20 17:25:21 +01:00
|
|
|
bool window_is_dragged;
|
2017-02-21 18:04:30 +01:00
|
|
|
id event_monitor_mouseup;
|
2017-02-10 15:33:01 +01:00
|
|
|
|
2014-10-05 19:16:23 +02:00
|
|
|
bool embedded; // wether we are embedding in another GUI
|
2012-09-16 20:53:04 +02:00
|
|
|
|
|
|
|
IOPMAssertionID power_mgmt_assertion;
|
2015-02-03 18:16:02 +01:00
|
|
|
io_connect_t light_sensor;
|
|
|
|
uint64_t last_lmuvalue;
|
|
|
|
int last_lux;
|
|
|
|
IONotificationPortRef light_sensor_io_port;
|
2013-01-20 17:15:37 +01:00
|
|
|
|
2013-08-01 08:23:53 +02:00
|
|
|
struct mp_log *log;
|
2014-01-06 14:17:14 +01:00
|
|
|
|
|
|
|
uint32_t old_dwidth;
|
|
|
|
uint32_t old_dheight;
|
2014-02-25 00:04:30 +01:00
|
|
|
|
2017-05-19 17:44:20 +02:00
|
|
|
pthread_mutex_t anim_lock;
|
|
|
|
pthread_cond_t anim_wakeup;
|
|
|
|
bool is_animating;
|
|
|
|
|
2016-12-17 20:53:09 +01:00
|
|
|
CVDisplayLinkRef link;
|
|
|
|
pthread_mutex_t sync_lock;
|
|
|
|
pthread_cond_t sync_wakeup;
|
|
|
|
uint64_t sync_counter;
|
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
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;
|
2015-05-12 22:31:03 +02:00
|
|
|
|
|
|
|
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
|
2015-08-10 22:14:39 +02:00
|
|
|
|
2015-12-06 18:39:09 +01:00
|
|
|
char *window_title;
|
2013-03-04 14:23:06 +01:00
|
|
|
};
|
2011-10-15 18:44:00 +02:00
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
static void run_on_main_thread(struct vo *vo, void(^block)(void))
|
2014-04-28 21:50:38 +02:00
|
|
|
{
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
dispatch_sync(dispatch_get_main_queue(), block);
|
2015-05-06 00:34:49 +02:00
|
|
|
}
|
|
|
|
|
2014-10-05 14:28:33 +02:00
|
|
|
static void queue_new_video_size(struct vo *vo, int w, int h)
|
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2017-01-14 17:10:08 +01:00
|
|
|
id<MpvWindowUpdate> win = (id<MpvWindowUpdate>) s->window;
|
2017-03-03 01:10:42 +01:00
|
|
|
NSRect r = NSMakeRect(0, 0, w, h);
|
|
|
|
r = [s->current_screen convertRectFromBacking:r];
|
|
|
|
[win queueNewVideoSize:r.size];
|
2014-10-05 14:28:33 +02:00
|
|
|
}
|
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-03-07 08:48:07 +01:00
|
|
|
static void enable_power_management(struct vo_cocoa_state *s)
|
2012-09-16 20:53:04 +02:00
|
|
|
{
|
|
|
|
if (!s->power_mgmt_assertion) return;
|
|
|
|
IOPMAssertionRelease(s->power_mgmt_assertion);
|
|
|
|
s->power_mgmt_assertion = kIOPMNullAssertionID;
|
|
|
|
}
|
|
|
|
|
2015-03-07 08:48:07 +01:00
|
|
|
static void disable_power_management(struct vo_cocoa_state *s)
|
2012-09-16 20:53:04 +02:00
|
|
|
{
|
|
|
|
if (s->power_mgmt_assertion) return;
|
2013-05-28 16:53:17 +02:00
|
|
|
IOPMAssertionCreateWithName(
|
|
|
|
kIOPMAssertionTypePreventUserIdleDisplaySleep,
|
|
|
|
kIOPMAssertionLevelOn,
|
2013-05-28 18:00:34 +02:00
|
|
|
CFSTR("io.mpv.video_playing_back"),
|
2013-05-28 16:53:17 +02:00
|
|
|
&s->power_mgmt_assertion);
|
2012-09-16 20:53:04 +02:00
|
|
|
}
|
|
|
|
|
2014-12-05 22:58:02 +01:00
|
|
|
static const char macosx_icon[] =
|
|
|
|
#include "osdep/macosx_icon.inc"
|
|
|
|
;
|
|
|
|
|
|
|
|
static void set_application_icon(NSApplication *app)
|
|
|
|
{
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
2015-05-25 08:58:32 +02:00
|
|
|
NSBundle *bundle = [NSBundle mainBundle];
|
|
|
|
if ([bundle pathForResource:@"icon" ofType:@"icns"])
|
|
|
|
return;
|
2018-07-01 11:56:08 +02:00
|
|
|
|
|
|
|
// The C string contains a trailing null, so we strip it away
|
2014-12-05 22:58:02 +01:00
|
|
|
NSData *icon_data = [NSData dataWithBytesNoCopy:(void *)macosx_icon
|
2018-07-01 11:56:08 +02:00
|
|
|
length:sizeof(macosx_icon) - 1
|
2014-12-05 22:58:02 +01:00
|
|
|
freeWhenDone:NO];
|
|
|
|
NSImage *icon = [[NSImage alloc] initWithData:icon_data];
|
|
|
|
[app setApplicationIconImage:icon];
|
|
|
|
[icon release];
|
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
|
2015-02-03 18:16:02 +01:00
|
|
|
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);
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
flag_events(vo, VO_EVENT_AMBIENT_LIGHTING_CHANGED);
|
2015-02-03 18:16:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cocoa_init_light_sensor(struct vo *vo)
|
|
|
|
{
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
run_on_main_thread(vo, ^{
|
2015-02-03 18:16:02 +01:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-03-07 08:48:07 +01:00
|
|
|
static void cocoa_uninit_light_sensor(struct vo_cocoa_state *s)
|
2015-02-03 18:16:02 +01:00
|
|
|
{
|
2015-03-07 19:27:13 +01:00
|
|
|
if (s->light_sensor_io_port) {
|
|
|
|
IONotificationPortDestroy(s->light_sensor_io_port);
|
|
|
|
IOObjectRelease(s->light_sensor);
|
|
|
|
}
|
2015-02-03 18:16:02 +01:00
|
|
|
}
|
|
|
|
|
2016-12-04 22:52:14 +01:00
|
|
|
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];
|
|
|
|
}
|
2017-02-13 18:37:51 +01:00
|
|
|
|
|
|
|
NSDictionary* sinfo = [s->current_screen deviceDescription];
|
|
|
|
s->display_id = [[sinfo objectForKey:@"NSScreenNumber"] longValue];
|
|
|
|
}
|
|
|
|
|
2017-05-19 17:44:20 +02:00
|
|
|
static void vo_cocoa_anim_lock(struct vo *vo)
|
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
|
|
|
pthread_mutex_lock(&s->anim_lock);
|
|
|
|
s->is_animating = true;
|
|
|
|
pthread_mutex_unlock(&s->anim_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vo_cocoa_anim_unlock(struct vo *vo)
|
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
|
|
|
pthread_mutex_lock(&s->anim_lock);
|
|
|
|
s->is_animating = false;
|
|
|
|
pthread_cond_signal(&s->anim_wakeup);
|
|
|
|
pthread_mutex_unlock(&s->anim_lock);
|
|
|
|
}
|
|
|
|
|
2017-02-13 18:37:51 +01:00
|
|
|
static void vo_cocoa_signal_swap(struct vo_cocoa_state *s)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&s->sync_lock);
|
|
|
|
s->sync_counter += 1;
|
|
|
|
pthread_cond_signal(&s->sync_wakeup);
|
|
|
|
pthread_mutex_unlock(&s->sync_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vo_cocoa_start_displaylink(struct vo_cocoa_state *s)
|
|
|
|
{
|
|
|
|
if (!CVDisplayLinkIsRunning(s->link))
|
|
|
|
CVDisplayLinkStart(s->link);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vo_cocoa_stop_displaylink(struct vo_cocoa_state *s)
|
|
|
|
{
|
|
|
|
if (CVDisplayLinkIsRunning(s->link)) {
|
|
|
|
CVDisplayLinkStop(s->link);
|
|
|
|
vo_cocoa_signal_swap(s);
|
|
|
|
}
|
2016-12-04 22:52:14 +01:00
|
|
|
}
|
|
|
|
|
2016-12-17 20:53:09 +01:00
|
|
|
static void vo_cocoa_init_displaylink(struct vo *vo)
|
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
|
|
|
|
2017-02-13 18:37:51 +01:00
|
|
|
CVDisplayLinkCreateWithCGDisplay(s->display_id, &s->link);
|
2016-12-17 20:53:09 +01:00
|
|
|
CVDisplayLinkSetOutputCallback(s->link, &displayLinkCallback, vo);
|
|
|
|
CVDisplayLinkStart(s->link);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vo_cocoa_uninit_displaylink(struct vo_cocoa_state *s)
|
|
|
|
{
|
2017-02-13 18:37:51 +01:00
|
|
|
vo_cocoa_stop_displaylink(s);
|
2016-12-17 20:53:09 +01:00
|
|
|
CVDisplayLinkRelease(s->link);
|
|
|
|
}
|
|
|
|
|
2017-02-21 18:04:30 +01:00
|
|
|
static void cocoa_add_event_monitor(struct vo *vo)
|
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
|
|
|
|
|
|
|
s->event_monitor_mouseup = [NSEvent
|
|
|
|
addLocalMonitorForEventsMatchingMask: NSEventMaskLeftMouseUp
|
|
|
|
handler:^NSEvent*(NSEvent* event) {
|
|
|
|
s->window_is_dragged = false;
|
|
|
|
return event;
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cocoa_rm_event_monitor(struct vo *vo)
|
|
|
|
{
|
|
|
|
[NSEvent removeMonitor:vo->cocoa->event_monitor_mouseup];
|
|
|
|
}
|
|
|
|
|
2015-10-01 22:25:12 +02:00
|
|
|
void vo_cocoa_init(struct vo *vo)
|
2011-10-15 18:44:00 +02:00
|
|
|
{
|
2015-03-07 08:48:07 +01:00
|
|
|
struct vo_cocoa_state *s = talloc_zero(NULL, struct vo_cocoa_state);
|
2013-09-03 21:18:28 +02:00
|
|
|
*s = (struct vo_cocoa_state){
|
|
|
|
.power_mgmt_assertion = kIOPMNullAssertionID,
|
|
|
|
.log = mp_log_new(s, vo->log, "cocoa"),
|
2014-10-05 19:16:23 +02:00
|
|
|
.embedded = vo->opts->WinID >= 0,
|
2017-02-10 15:33:01 +01:00
|
|
|
.cursor_visibility = true,
|
|
|
|
.cursor_visibility_wanted = true,
|
2016-12-04 22:52:14 +01:00
|
|
|
.fullscreen = 0,
|
2013-09-03 21:18:28 +02:00
|
|
|
};
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
pthread_mutex_init(&s->lock, NULL);
|
|
|
|
pthread_cond_init(&s->wakeup, NULL);
|
2016-12-17 20:53:09 +01:00
|
|
|
pthread_mutex_init(&s->sync_lock, NULL);
|
|
|
|
pthread_cond_init(&s->sync_wakeup, NULL);
|
2017-05-19 17:44:20 +02:00
|
|
|
pthread_mutex_init(&s->anim_lock, NULL);
|
|
|
|
pthread_cond_init(&s->anim_wakeup, NULL);
|
2013-09-03 21:18:28 +02:00
|
|
|
vo->cocoa = s;
|
2016-12-04 22:52:14 +01:00
|
|
|
vo_cocoa_update_screen_info(vo);
|
2017-01-18 17:22:32 +01:00
|
|
|
vo_cocoa_init_displaylink(vo);
|
2015-03-07 09:11:47 +01:00
|
|
|
cocoa_init_light_sensor(vo);
|
2016-01-06 13:27:33 +01:00
|
|
|
cocoa_add_screen_reconfiguration_observer(vo);
|
2017-02-21 18:04:30 +01:00
|
|
|
cocoa_add_event_monitor(vo);
|
|
|
|
|
2015-10-11 16:13:28 +02:00
|
|
|
if (!s->embedded) {
|
2020-02-22 12:22:16 +01:00
|
|
|
run_on_main_thread(vo, ^{
|
|
|
|
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
|
|
|
|
set_application_icon(NSApp);
|
|
|
|
});
|
2015-10-11 16:13:28 +02:00
|
|
|
}
|
2011-10-15 18:44:00 +02:00
|
|
|
}
|
|
|
|
|
2017-02-18 00:46:06 +01:00
|
|
|
static int vo_cocoa_update_cursor_visibility(struct vo *vo, bool forceVisible)
|
2013-05-17 00:29:42 +02:00
|
|
|
{
|
2013-06-01 00:35:46 +02:00
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
|
|
|
|
2014-10-08 19:23:47 +02:00
|
|
|
if (s->embedded)
|
|
|
|
return VO_NOTIMPL;
|
|
|
|
|
2017-02-10 15:33:01 +01:00
|
|
|
if (s->view) {
|
|
|
|
MpvEventsView *v = (MpvEventsView *) s->view;
|
2017-02-18 00:46:06 +01:00
|
|
|
bool visibility = !(!s->cursor_visibility_wanted && [v canHideCursor]);
|
|
|
|
|
|
|
|
if ((forceVisible || visibility) && !s->cursor_visibility) {
|
|
|
|
[NSCursor unhide];
|
|
|
|
s->cursor_visibility = YES;
|
|
|
|
} else if (!visibility && s->cursor_visibility) {
|
|
|
|
[NSCursor hide];
|
|
|
|
s->cursor_visibility = NO;
|
|
|
|
}
|
2013-05-17 00:29:42 +02:00
|
|
|
}
|
2014-10-08 19:23:47 +02:00
|
|
|
return VO_TRUE;
|
2013-05-17 00:29:42 +02:00
|
|
|
}
|
|
|
|
|
2011-10-15 18:44:00 +02:00
|
|
|
void vo_cocoa_uninit(struct vo *vo)
|
|
|
|
{
|
2015-01-01 14:38:08 +01:00
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
pthread_mutex_lock(&s->lock);
|
2015-05-12 22:31:03 +02:00
|
|
|
s->vo_ready = false;
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
pthread_cond_signal(&s->wakeup);
|
|
|
|
pthread_mutex_unlock(&s->lock);
|
2015-05-12 22:31:03 +02:00
|
|
|
|
2017-05-19 17:44:20 +02:00
|
|
|
pthread_mutex_lock(&s->anim_lock);
|
|
|
|
while(s->is_animating)
|
|
|
|
pthread_cond_wait(&s->anim_wakeup, &s->anim_lock);
|
|
|
|
pthread_mutex_unlock(&s->anim_lock);
|
|
|
|
|
2016-12-29 20:04:44 +01:00
|
|
|
// 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) {
|
2017-02-18 00:46:06 +01:00
|
|
|
vo_cocoa_update_cursor_visibility(vo, true);
|
2016-12-29 20:04:44 +01:00
|
|
|
[s->window setDelegate:nil];
|
|
|
|
[s->window close];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
run_on_main_thread(vo, ^{
|
2015-03-07 08:48:07 +01:00
|
|
|
enable_power_management(s);
|
2016-12-17 20:53:09 +01:00
|
|
|
vo_cocoa_uninit_displaylink(s);
|
2017-02-13 18:37:51 +01:00
|
|
|
vo_cocoa_signal_swap(s);
|
2015-03-07 08:48:07 +01:00
|
|
|
cocoa_uninit_light_sensor(s);
|
2016-01-06 13:27:33 +01:00
|
|
|
cocoa_rm_screen_reconfiguration_observer(vo);
|
2017-02-21 18:04:30 +01:00
|
|
|
cocoa_rm_event_monitor(vo);
|
2011-10-15 18:44:00 +02:00
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
[s->nsgl_ctx release];
|
|
|
|
CGLReleaseContext(s->cgl_ctx);
|
2015-01-01 14:38:08 +01:00
|
|
|
|
|
|
|
// needed to stop resize events triggered by the event's view -clear
|
|
|
|
// causing many uses after free
|
|
|
|
[s->video removeFromSuperview];
|
|
|
|
|
2014-10-27 19:10:52 +01:00
|
|
|
[s->view removeFromSuperview];
|
2014-11-25 14:44:27 +01:00
|
|
|
[s->view release];
|
2014-12-06 21:54:51 +01:00
|
|
|
|
2017-05-19 17:44:20 +02:00
|
|
|
pthread_cond_destroy(&s->anim_wakeup);
|
|
|
|
pthread_mutex_destroy(&s->anim_lock);
|
2016-12-17 20:53:09 +01:00
|
|
|
pthread_cond_destroy(&s->sync_wakeup);
|
|
|
|
pthread_mutex_destroy(&s->sync_lock);
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
pthread_cond_destroy(&s->wakeup);
|
|
|
|
pthread_mutex_destroy(&s->lock);
|
2015-03-07 08:48:07 +01:00
|
|
|
talloc_free(s);
|
|
|
|
});
|
2011-10-15 18:44:00 +02:00
|
|
|
}
|
|
|
|
|
2017-01-27 20:41:40 +01:00
|
|
|
static void vo_cocoa_update_displaylink(struct vo *vo)
|
2015-02-03 21:43:20 +01:00
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2015-02-03 22:59:54 +01:00
|
|
|
|
2017-01-27 20:41:40 +01:00
|
|
|
vo_cocoa_uninit_displaylink(s);
|
|
|
|
vo_cocoa_init_displaylink(vo);
|
|
|
|
}
|
2016-01-06 13:33:42 +01:00
|
|
|
|
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;
|
2016-06-16 19:28:14 +02:00
|
|
|
}
|
2015-02-03 21:43:20 +01:00
|
|
|
}
|
2015-03-14 12:33:09 +01:00
|
|
|
|
2017-01-27 20:41:40 +01:00
|
|
|
MP_WARN(vo, "Falling back to standard display refresh rate: 60Hz\n");
|
|
|
|
return 60.0;
|
2015-02-03 21:43:20 +01:00
|
|
|
}
|
|
|
|
|
2016-06-16 19:28:14 +02:00
|
|
|
static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now,
|
|
|
|
const CVTimeStamp* outputTime, CVOptionFlags flagsIn,
|
|
|
|
CVOptionFlags* flagsOut, void* displayLinkContext)
|
|
|
|
{
|
2016-12-17 20:53:09 +01:00
|
|
|
struct vo *vo = displayLinkContext;
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
|
|
|
|
2017-02-13 18:37:51 +01:00
|
|
|
vo_cocoa_signal_swap(s);
|
2016-06-16 19:28:14 +02:00
|
|
|
return kCVReturnSuccess;
|
|
|
|
}
|
|
|
|
|
2017-02-13 20:08:43 +01:00
|
|
|
static void vo_set_level(struct vo *vo, int ontop, int ontop_level)
|
2011-12-07 12:42:04 +01:00
|
|
|
{
|
2012-09-13 09:32:59 +02:00
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2014-10-05 14:28:33 +02:00
|
|
|
|
2011-12-07 12:42:04 +01:00
|
|
|
if (ontop) {
|
2017-02-13 20:08:43 +01:00
|
|
|
switch (ontop_level) {
|
|
|
|
case -1:
|
|
|
|
s->window_level = NSFloatingWindowLevel;
|
|
|
|
break;
|
|
|
|
case -2:
|
|
|
|
s->window_level = NSStatusWindowLevel;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
s->window_level = ontop_level;
|
|
|
|
}
|
2011-12-07 12:42:04 +01:00
|
|
|
} else {
|
2012-09-30 00:32:53 +02:00
|
|
|
s->window_level = NSNormalWindowLevel;
|
2011-12-07 12:42:04 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 20:51:35 +01:00
|
|
|
[s->window setLevel:s->window_level];
|
|
|
|
NSWindowCollectionBehavior behavior = [s->window collectionBehavior] &
|
|
|
|
~NSWindowCollectionBehaviorTransient;
|
|
|
|
[s->window setCollectionBehavior:behavior|NSWindowCollectionBehaviorManaged];
|
2011-12-07 12:42:04 +01:00
|
|
|
}
|
|
|
|
|
2014-10-05 19:16:23 +02:00
|
|
|
static int vo_cocoa_ontop(struct vo *vo)
|
2011-12-07 12:42:04 +01:00
|
|
|
{
|
2014-10-05 19:16:23 +02:00
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
|
|
|
if (s->embedded)
|
|
|
|
return VO_NOTIMPL;
|
|
|
|
|
2013-03-04 22:41:27 +01:00
|
|
|
struct mp_vo_opts *opts = vo->opts;
|
2017-02-13 20:08:43 +01:00
|
|
|
vo_set_level(vo, opts->ontop, opts->ontop_level);
|
2014-10-05 19:16:23 +02:00
|
|
|
return VO_TRUE;
|
2011-12-07 12:42:04 +01:00
|
|
|
}
|
|
|
|
|
2014-10-05 14:28:33 +02:00
|
|
|
static MpvVideoWindow *create_window(NSRect rect, NSScreen *s, bool border,
|
|
|
|
MpvCocoaAdapter *adapter)
|
2013-06-01 11:26:06 +02:00
|
|
|
{
|
2013-05-29 00:14:34 +02:00
|
|
|
int window_mask = 0;
|
2014-10-05 14:28:33 +02:00
|
|
|
if (border) {
|
2016-09-20 23:30:54 +02:00
|
|
|
window_mask = NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|
|
|
|
|
NSWindowStyleMaskMiniaturizable|NSWindowStyleMaskResizable;
|
2013-05-29 00:14:34 +02:00
|
|
|
} else {
|
2016-09-20 23:30:54 +02:00
|
|
|
window_mask = NSWindowStyleMaskBorderless|NSWindowStyleMaskResizable;
|
2013-05-29 00:14:34 +02:00
|
|
|
}
|
|
|
|
|
2014-10-05 14:28:33 +02:00
|
|
|
MpvVideoWindow *w =
|
|
|
|
[[MpvVideoWindow alloc] initWithContentRect:rect
|
2013-09-03 21:18:28 +02:00
|
|
|
styleMask:window_mask
|
|
|
|
backing:NSBackingStoreBuffered
|
2014-01-11 13:56:10 +01:00
|
|
|
defer:NO
|
2014-10-05 14:28:33 +02:00
|
|
|
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;
|
|
|
|
|
2014-10-27 19:10:52 +01:00
|
|
|
MpvCocoaAdapter *adapter = [[MpvCocoaAdapter alloc] init];
|
2014-10-08 19:23:47 +02:00
|
|
|
adapter.vout = vo;
|
2014-10-05 14:28:33 +02:00
|
|
|
|
2014-10-12 00:17:48 +02:00
|
|
|
NSView *parent;
|
2014-10-05 19:16:23 +02:00
|
|
|
if (s->embedded) {
|
2014-10-12 00:17:48 +02:00
|
|
|
parent = (NSView *) (intptr_t) opts->WinID;
|
2014-10-05 14:28:33 +02:00
|
|
|
} else {
|
2017-03-03 01:10:42 +01:00
|
|
|
NSRect wr = NSMakeRect(win->x0, win->y1, win->x1 - win->x0, win->y0 - win->y1);
|
|
|
|
wr = [s->current_screen convertRectFromBacking:wr];
|
2014-10-12 00:17:48 +02:00
|
|
|
s->window = create_window(wr, s->current_screen, opts->border, adapter);
|
|
|
|
parent = [s->window contentView];
|
2014-10-05 14:28:33 +02:00
|
|
|
}
|
2012-10-15 22:03:08 +02:00
|
|
|
|
2014-10-12 00:17:48 +02:00
|
|
|
MpvEventsView *view = [[MpvEventsView alloc] initWithFrame:[parent bounds]];
|
|
|
|
view.adapter = adapter;
|
|
|
|
s->view = view;
|
|
|
|
[parent addSubview:s->view];
|
2015-03-08 15:19:17 +01:00
|
|
|
s->adapter = adapter;
|
2014-10-12 00:17:48 +02:00
|
|
|
|
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:));
|
2012-10-15 22:03:08 +02:00
|
|
|
|
2014-10-05 22:31:33 +02:00
|
|
|
s->video = [[MpvVideoView alloc] initWithFrame:[s->view bounds]];
|
2014-10-06 09:58:22 +02:00
|
|
|
[s->video setWantsBestResolutionOpenGLSurface:YES];
|
|
|
|
|
2014-10-05 22:31:33 +02:00
|
|
|
[s->view addSubview:s->video];
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
[s->nsgl_ctx setView:s->video];
|
2014-10-27 19:10:52 +01:00
|
|
|
[s->video release];
|
2013-09-03 21:18:28 +02:00
|
|
|
|
2014-10-05 22:31:33 +02:00
|
|
|
s->video.adapter = adapter;
|
2014-10-27 19:18:29 +01:00
|
|
|
[adapter release];
|
2012-10-15 22:03:08 +02:00
|
|
|
|
2014-10-05 19:16:23 +02:00
|
|
|
if (!s->embedded) {
|
2014-10-05 14:28:33 +02:00
|
|
|
[s->window setRestorable:NO];
|
|
|
|
[s->window makeMainWindow];
|
|
|
|
[s->window makeKeyAndOrderFront:nil];
|
2017-02-20 17:23:29 +01:00
|
|
|
if (!opts->fullscreen)
|
|
|
|
[s->window setMovableByWindowBackground:YES];
|
2014-10-05 14:28:33 +02:00
|
|
|
[NSApp activateIgnoringOtherApps:YES];
|
|
|
|
}
|
2012-10-15 22:03:08 +02:00
|
|
|
}
|
|
|
|
|
2015-12-06 18:39:09 +01:00
|
|
|
static int cocoa_set_window_title(struct vo *vo)
|
2013-06-16 10:57:36 +02:00
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2014-10-05 19:16:23 +02:00
|
|
|
if (s->embedded)
|
|
|
|
return VO_NOTIMPL;
|
2014-10-05 14:28:33 +02:00
|
|
|
|
2013-12-25 17:58:34 +01:00
|
|
|
void *talloc_ctx = talloc_new(NULL);
|
2015-12-06 18:39:09 +01:00
|
|
|
struct bstr btitle =
|
|
|
|
bstr_sanitize_utf8_latin1(talloc_ctx, bstr0(s->window_title));
|
2015-12-07 00:49:36 +01:00
|
|
|
if (btitle.start) {
|
|
|
|
NSString *nstitle = [NSString stringWithUTF8String:btitle.start];
|
|
|
|
if (nstitle) {
|
|
|
|
[s->window setTitle: nstitle];
|
|
|
|
[s->window displayIfNeeded];
|
|
|
|
}
|
2014-12-20 11:43:42 +01:00
|
|
|
}
|
2013-12-25 17:58:34 +01:00
|
|
|
talloc_free(talloc_ctx);
|
2014-10-05 19:16:23 +02:00
|
|
|
return VO_TRUE;
|
2013-06-16 10:57:36 +02:00
|
|
|
}
|
|
|
|
|
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;
|
2017-01-14 17:10:08 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-01-06 13:27:33 +01:00
|
|
|
static void cocoa_screen_reconfiguration_observer(
|
|
|
|
CGDirectDisplayID display, CGDisplayChangeSummaryFlags flags, void *ctx)
|
|
|
|
{
|
|
|
|
if (flags & kCGDisplaySetModeFlag) {
|
|
|
|
struct vo *vo = ctx;
|
2017-01-28 16:29:22 +01:00
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
|
|
|
|
2017-02-13 18:37:51 +01:00
|
|
|
if (s->display_id == display) {
|
2017-01-28 16:29:22 +01:00
|
|
|
MP_VERBOSE(vo, "detected display mode change, updating screen refresh rate\n");
|
|
|
|
flag_events(vo, VO_EVENT_WIN_STATE);
|
|
|
|
}
|
2016-01-06 13:27:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
void vo_cocoa_set_opengl_ctx(struct vo *vo, CGLContextObj ctx)
|
2014-07-08 08:17:15 +02:00
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
run_on_main_thread(vo, ^{
|
|
|
|
s->cgl_ctx = CGLRetainContext(ctx);
|
|
|
|
s->nsgl_ctx = [[NSOpenGLContext alloc] initWithCGLContextObj:s->cgl_ctx];
|
|
|
|
});
|
2014-07-08 08:17:15 +02:00
|
|
|
}
|
|
|
|
|
2015-10-01 22:25:12 +02:00
|
|
|
int vo_cocoa_config_window(struct vo *vo)
|
2012-10-15 22:03:08 +02:00
|
|
|
{
|
2013-05-12 23:51:58 +02:00
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2016-12-04 22:52:14 +01:00
|
|
|
struct mp_vo_opts *opts = vo->opts;
|
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
run_on_main_thread(vo, ^{
|
2016-12-04 22:52:14 +01:00
|
|
|
NSRect r = [s->current_screen frame];
|
2017-03-03 01:10:42 +01:00
|
|
|
r = [s->current_screen convertRectToBacking:r];
|
2016-12-04 22:52:14 +01:00
|
|
|
struct mp_rect screenrc = {0, 0, r.size.width, r.size.height};
|
2014-05-06 22:00:17 +02:00
|
|
|
struct vo_win_geometry geo;
|
2017-03-03 01:10:42 +01:00
|
|
|
vo_calc_window_geometry2(vo, &screenrc, [s->current_screen backingScaleFactor], &geo);
|
2014-05-06 22:00:17 +02:00
|
|
|
vo_apply_window_geometry(vo, &geo);
|
|
|
|
|
2017-03-03 01:10:42 +01:00
|
|
|
//flip y coordinates
|
|
|
|
geo.win.y1 = r.size.height - geo.win.y1;
|
|
|
|
geo.win.y0 = r.size.height - geo.win.y0;
|
|
|
|
|
2014-05-06 22:00:17 +02:00
|
|
|
uint32_t width = vo->dwidth;
|
|
|
|
uint32_t height = vo->dheight;
|
|
|
|
|
2014-01-06 14:17:14 +01:00
|
|
|
bool reset_size = s->old_dwidth != width || s->old_dheight != height;
|
|
|
|
s->old_dwidth = width;
|
|
|
|
s->old_dheight = height;
|
|
|
|
|
2015-10-01 22:25:12 +02:00
|
|
|
if (!s->view) {
|
2014-10-05 14:28:33 +02:00
|
|
|
create_ui(vo, &geo.win, geo.flags);
|
|
|
|
}
|
2014-07-15 21:33:19 +02:00
|
|
|
|
2017-03-14 02:38:54 +01:00
|
|
|
s->unfs_window = NSMakeRect(0, 0, width, height);
|
|
|
|
|
2014-10-05 22:38:26 +02:00
|
|
|
if (!s->embedded && s->window) {
|
2014-01-06 14:17:14 +01:00
|
|
|
if (reset_size)
|
2014-10-05 14:28:33 +02:00
|
|
|
queue_new_video_size(vo, width, height);
|
2016-12-04 22:52:14 +01:00
|
|
|
if (opts->fullscreen && !s->fullscreen)
|
|
|
|
vo_cocoa_fullscreen(vo);
|
2015-12-06 18:39:09 +01:00
|
|
|
cocoa_set_window_title(vo);
|
2017-02-13 20:08:43 +01:00
|
|
|
vo_set_level(vo, opts->ontop, opts->ontop_level);
|
2015-12-19 09:30:20 +01:00
|
|
|
|
|
|
|
GLint o;
|
|
|
|
if (!CGLGetParameter(s->cgl_ctx, kCGLCPSurfaceOpacity, &o) && !o) {
|
|
|
|
[s->window setOpaque:NO];
|
|
|
|
[s->window setBackgroundColor:[NSColor clearColor]];
|
|
|
|
}
|
2013-05-11 00:22:23 +02:00
|
|
|
}
|
2014-10-05 14:28:33 +02:00
|
|
|
|
2015-05-12 22:31:03 +02:00
|
|
|
s->vo_ready = true;
|
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
// 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;
|
2014-12-05 22:58:02 +01:00
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
[s->nsgl_ctx update];
|
|
|
|
});
|
2013-06-01 11:26:06 +02:00
|
|
|
return 0;
|
2013-03-04 14:23:06 +01:00
|
|
|
}
|
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
// 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)
|
2013-03-04 14:23:06 +01:00
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
NSRect frame = [s->video frameInPixels];
|
2013-09-28 14:53:55 +02:00
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
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);
|
2011-10-15 18:44:00 +02:00
|
|
|
}
|
|
|
|
|
2014-10-18 18:30:22 +02:00
|
|
|
static void vo_cocoa_resize_redraw(struct vo *vo, int width, int height)
|
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
resize_event(vo);
|
2014-10-18 18:30:22 +02:00
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
pthread_mutex_lock(&s->lock);
|
2014-10-18 18:30:22 +02:00
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
// 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));
|
2015-05-12 22:31:03 +02:00
|
|
|
while (s->frame_w != width && s->frame_h != height && s->vo_ready) {
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
if (pthread_cond_timedwait(&s->wakeup, &s->lock, &e))
|
2015-05-12 22:31:03 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
pthread_mutex_unlock(&s->lock);
|
2014-10-18 18:30:22 +02:00
|
|
|
}
|
|
|
|
|
2015-05-12 22:31:03 +02:00
|
|
|
void vo_cocoa_swap_buffers(struct vo *vo)
|
2015-05-01 18:25:29 +02:00
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
|
|
|
|
2015-05-12 22:31:03 +02:00
|
|
|
// Don't swap a frame with wrong size
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
pthread_mutex_lock(&s->lock);
|
|
|
|
bool skip = s->pending_events & VO_EVENT_RESIZE;
|
|
|
|
pthread_mutex_unlock(&s->lock);
|
|
|
|
if (skip)
|
2015-05-12 22:31:03 +02:00
|
|
|
return;
|
2015-05-01 18:25:29 +02:00
|
|
|
|
2016-12-17 20:53:09 +01:00
|
|
|
pthread_mutex_lock(&s->sync_lock);
|
|
|
|
uint64_t old_counter = s->sync_counter;
|
2017-02-13 18:37:51 +01:00
|
|
|
while(CVDisplayLinkIsRunning(s->link) && old_counter == s->sync_counter) {
|
2016-12-17 20:53:09 +01:00
|
|
|
pthread_cond_wait(&s->sync_wakeup, &s->sync_lock);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&s->sync_lock);
|
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
pthread_mutex_lock(&s->lock);
|
2015-05-12 22:31:03 +02:00
|
|
|
s->frame_w = vo->dwidth;
|
|
|
|
s->frame_h = vo->dheight;
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
pthread_cond_signal(&s->wakeup);
|
|
|
|
pthread_mutex_unlock(&s->lock);
|
2011-10-15 18:44:00 +02:00
|
|
|
}
|
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
static int vo_cocoa_check_events(struct vo *vo)
|
2011-10-15 18:44:00 +02:00
|
|
|
{
|
2012-09-13 09:32:59 +02:00
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
|
|
|
|
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;
|
2014-10-05 22:31:33 +02:00
|
|
|
if (events & VO_EVENT_RESIZE) {
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
vo->dwidth = s->vo_dwidth;
|
|
|
|
vo->dheight = s->vo_dheight;
|
2014-10-05 22:31:33 +02:00
|
|
|
}
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
pthread_mutex_unlock(&s->lock);
|
2013-01-20 16:53:51 +01:00
|
|
|
|
2014-10-05 19:31:21 +02:00
|
|
|
return events;
|
2011-10-15 18:44:00 +02:00
|
|
|
}
|
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
static int vo_cocoa_fullscreen(struct vo *vo)
|
2011-10-15 18:44:00 +02:00
|
|
|
{
|
2012-09-13 09:32:59 +02:00
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2013-09-03 21:18:28 +02:00
|
|
|
|
2014-10-08 19:23:47 +02:00
|
|
|
if (s->embedded)
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
return VO_NOTIMPL;
|
2014-10-08 19:23:47 +02:00
|
|
|
|
2017-03-14 02:38:54 +01:00
|
|
|
if (!s->fullscreen)
|
|
|
|
s->unfs_window = [s->view frame];
|
|
|
|
|
2016-12-04 22:52:14 +01:00
|
|
|
[s->window toggleFullScreen:nil];
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
|
|
|
|
return VO_TRUE;
|
2011-10-15 18:44:00 +02:00
|
|
|
}
|
|
|
|
|
2015-01-07 18:47:27 +01:00
|
|
|
static void vo_cocoa_control_get_icc_profile(struct vo *vo, void *arg)
|
2014-02-25 00:04:30 +01:00
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2015-01-07 18:47:27 +01:00
|
|
|
bstr *p = arg;
|
2014-02-25 00:04:30 +01:00
|
|
|
|
2016-12-04 22:52:14 +01:00
|
|
|
NSData *profile = [[s->current_screen colorSpace] ICCProfileData];
|
2014-02-25 00:04:30 +01:00
|
|
|
|
2015-01-07 18:47:27 +01:00
|
|
|
p->start = talloc_memdup(NULL, (void *)[profile bytes], [profile length]);
|
|
|
|
p->len = [profile length];
|
2014-02-25 00:04:30 +01:00
|
|
|
}
|
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
static int vo_cocoa_control_on_main_thread(struct vo *vo, int request, void *arg)
|
2013-05-15 18:17:18 +02:00
|
|
|
{
|
2016-12-04 22:52:14 +01:00
|
|
|
struct vo_cocoa_state *s = vo->cocoa;
|
2015-01-17 02:45:17 +01:00
|
|
|
|
2013-05-15 18:17:18 +02:00
|
|
|
switch (request) {
|
2014-09-04 22:53:50 +02:00
|
|
|
case VOCTRL_GET_UNFS_WINDOW_SIZE: {
|
2016-12-04 22:52:14 +01:00
|
|
|
int *sz = arg;
|
2017-04-15 22:33:49 +02:00
|
|
|
NSRect rect = (s->fullscreen || vo->opts->fullscreen) ?
|
|
|
|
s->unfs_window : [s->view frame];
|
2017-03-14 02:38:54 +01:00
|
|
|
if(!vo->opts->hidpi_window_scale)
|
|
|
|
rect = [s->current_screen convertRectToBacking:rect];
|
|
|
|
sz[0] = rect.size.width;
|
|
|
|
sz[1] = rect.size.height;
|
2013-11-22 08:41:34 +01:00
|
|
|
return VO_TRUE;
|
|
|
|
}
|
2014-09-04 22:53:50 +02:00
|
|
|
case VOCTRL_SET_UNFS_WINDOW_SIZE: {
|
2016-12-04 22:52:14 +01:00
|
|
|
int *sz = arg;
|
2017-03-03 01:10:42 +01:00
|
|
|
NSRect r = NSMakeRect(0, 0, sz[0], sz[1]);
|
|
|
|
if(vo->opts->hidpi_window_scale)
|
|
|
|
r = [s->current_screen convertRectToBacking:r];
|
|
|
|
queue_new_video_size(vo, r.size.width, r.size.height);
|
2013-11-22 08:41:34 +01:00
|
|
|
return VO_TRUE;
|
|
|
|
}
|
2013-09-08 09:16:19 +02:00
|
|
|
case VOCTRL_SET_CURSOR_VISIBILITY:
|
2017-02-18 00:46:06 +01:00
|
|
|
s->cursor_visibility_wanted = *(bool *)arg;
|
|
|
|
return vo_cocoa_update_cursor_visibility(vo, false);
|
2015-12-06 18:39:09 +01:00
|
|
|
case VOCTRL_UPDATE_WINDOW_TITLE: {
|
|
|
|
talloc_free(s->window_title);
|
|
|
|
s->window_title = talloc_strdup(s, (char *) arg);
|
|
|
|
return cocoa_set_window_title(vo);
|
|
|
|
}
|
2013-06-14 00:03:32 +02:00
|
|
|
case VOCTRL_RESTORE_SCREENSAVER:
|
2016-12-04 22:52:14 +01:00
|
|
|
enable_power_management(s);
|
2013-05-15 18:17:18 +02:00
|
|
|
return VO_TRUE;
|
2013-06-14 00:03:32 +02:00
|
|
|
case VOCTRL_KILL_SCREENSAVER:
|
2016-12-04 22:52:14 +01:00
|
|
|
disable_power_management(s);
|
2013-05-15 18:17:18 +02:00
|
|
|
return VO_TRUE;
|
2015-01-07 18:47:27 +01:00
|
|
|
case VOCTRL_GET_ICC_PROFILE:
|
|
|
|
vo_cocoa_control_get_icc_profile(vo, arg);
|
2014-02-25 00:04:30 +01:00
|
|
|
return VO_TRUE;
|
2015-02-03 21:43:20 +01:00
|
|
|
case VOCTRL_GET_DISPLAY_FPS:
|
2017-01-27 20:41:40 +01:00
|
|
|
*(double *)arg = vo_cocoa_update_screen_fps(vo);
|
|
|
|
return VO_TRUE;
|
2015-05-11 21:05:34 +02:00
|
|
|
break;
|
2015-02-03 18:16:02 +01:00
|
|
|
case VOCTRL_GET_AMBIENT_LUX:
|
2016-12-04 22:52:14 +01:00
|
|
|
if (s->light_sensor != IO_OBJECT_NULL) {
|
|
|
|
*(int *)arg = s->last_lux;
|
2015-02-03 18:16:02 +01:00
|
|
|
return VO_TRUE;
|
|
|
|
}
|
2015-05-11 21:05:34 +02:00
|
|
|
break;
|
2013-05-15 18:17:18 +02:00
|
|
|
}
|
|
|
|
return VO_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-09-03 21:18:28 +02:00
|
|
|
@implementation MpvCocoaAdapter
|
|
|
|
@synthesize vout = _video_output;
|
2013-07-20 11:22:50 +02:00
|
|
|
|
2016-12-15 23:06:04 +01:00
|
|
|
- (void)performAsyncResize:(NSSize)size
|
|
|
|
{
|
2016-12-04 22:52:14 +01:00
|
|
|
vo_cocoa_resize_redraw(self.vout, size.width, size.height);
|
2014-10-04 11:47:17 +02:00
|
|
|
}
|
|
|
|
|
2016-12-15 23:06:04 +01:00
|
|
|
- (BOOL)keyboardEnabled
|
|
|
|
{
|
2014-10-17 19:15:17 +02:00
|
|
|
return !!mp_input_vo_keyboard_enabled(self.vout->input_ctx);
|
|
|
|
}
|
|
|
|
|
2016-12-15 23:06:04 +01:00
|
|
|
- (BOOL)mouseEnabled
|
|
|
|
{
|
2014-10-17 19:15:17 +02:00
|
|
|
return !!mp_input_mouse_enabled(self.vout->input_ctx);
|
|
|
|
}
|
|
|
|
|
2016-12-15 23:06:04 +01:00
|
|
|
- (void)setNeedsResize
|
|
|
|
{
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
resize_event(self.vout);
|
2013-09-02 08:21:03 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 21:18:28 +02:00
|
|
|
- (void)recalcMovableByWindowBackground:(NSPoint)p
|
2013-06-22 10:42:50 +02:00
|
|
|
{
|
2013-09-03 21:18:28 +02:00
|
|
|
BOOL movable = NO;
|
2016-12-04 22:52:14 +01:00
|
|
|
if (!self.vout->cocoa->fullscreen) {
|
2013-09-03 21:18:28 +02:00
|
|
|
movable = !mp_input_test_dragging(self.vout->input_ctx, p.x, p.y);
|
2013-06-22 10:42:50 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 21:18:28 +02:00
|
|
|
[self.vout->cocoa->window setMovableByWindowBackground:movable];
|
2011-12-08 11:17:59 +01:00
|
|
|
}
|
|
|
|
|
2016-12-15 23:06:04 +01:00
|
|
|
- (void)signalMouseMovement:(NSPoint)point
|
|
|
|
{
|
2014-10-17 19:15:17 +02:00
|
|
|
[self recalcMovableByWindowBackground:point];
|
2017-02-20 17:25:21 +01:00
|
|
|
if (!self.vout->cocoa->window_is_dragged)
|
|
|
|
mp_input_set_mouse_pos(self.vout->input_ctx, point.x, point.y);
|
2011-10-15 18:44:00 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 21:18:28 +02:00
|
|
|
- (void)putKey:(int)mpkey withModifiers:(int)modifiers
|
2013-07-21 10:33:18 +02:00
|
|
|
{
|
2014-10-17 19:15:17 +02:00
|
|
|
cocoa_put_key_with_modifiers(mpkey, modifiers);
|
2013-07-21 10:33:18 +02:00
|
|
|
}
|
|
|
|
|
2017-09-02 16:00:52 +02:00
|
|
|
- (void)putWheel:(int)mpkey delta:(float)delta;
|
2013-07-21 10:33:18 +02:00
|
|
|
{
|
2017-09-02 16:00:52 +02:00
|
|
|
mp_input_put_wheel(self.vout->input_ctx, mpkey, delta);
|
2013-07-21 10:33:18 +02:00
|
|
|
}
|
|
|
|
|
2013-11-22 08:43:02 +01:00
|
|
|
- (void)putCommand:(char*)cmd
|
|
|
|
{
|
2014-03-13 04:34:56 +01:00
|
|
|
char *cmd_ = ta_strdup(NULL, cmd);
|
|
|
|
mp_cmd_t *cmdt = mp_input_parse_cmd(self.vout->input_ctx, bstr0(cmd_), "");
|
2013-11-22 08:43:02 +01:00
|
|
|
mp_input_queue_cmd(self.vout->input_ctx, cmdt);
|
2014-03-13 04:34:56 +01:00
|
|
|
ta_free(cmd_);
|
2013-11-22 08:43:02 +01:00
|
|
|
}
|
|
|
|
|
2016-12-04 22:52:14 +01:00
|
|
|
- (BOOL)isInFullScreenMode
|
|
|
|
{
|
|
|
|
return self.vout->cocoa->fullscreen;
|
2011-10-15 18:44:00 +02:00
|
|
|
}
|
2013-06-01 22:52:21 +02:00
|
|
|
|
2017-07-27 21:28:33 +02:00
|
|
|
- (BOOL)wantsNativeFullscreen
|
|
|
|
{
|
|
|
|
return self.vout->opts->native_fs;
|
|
|
|
}
|
|
|
|
|
2016-12-04 22:52:14 +01:00
|
|
|
- (NSScreen *)getTargetScreen
|
|
|
|
{
|
2013-09-03 21:18:28 +02:00
|
|
|
struct vo_cocoa_state *s = self.vout->cocoa;
|
2016-12-04 22:52:14 +01:00
|
|
|
struct mp_vo_opts *opts = self.vout->opts;
|
2014-01-04 17:17:33 +01:00
|
|
|
|
2016-12-04 22:52:14 +01:00
|
|
|
int screen_id = s->fullscreen ? opts->screen_id : opts->fsscreen_id;
|
|
|
|
return get_screen_by_id(self.vout, screen_id);
|
2014-12-01 18:05:30 +01:00
|
|
|
}
|
|
|
|
|
2014-01-04 17:17:33 +01:00
|
|
|
- (void)handleFilesArray:(NSArray *)files
|
|
|
|
{
|
2014-04-16 04:38:19 +02:00
|
|
|
[[EventsResponder sharedInstance] handleFilesArray:files];
|
2014-01-04 17:17:33 +01:00
|
|
|
}
|
2014-02-25 00:04:30 +01:00
|
|
|
|
2016-06-17 04:02:51 +02:00
|
|
|
- (void)windowDidChangeScreen:(NSNotification *)notification
|
|
|
|
{
|
2016-12-04 22:52:14 +01:00
|
|
|
vo_cocoa_update_screen_info(self.vout);
|
2017-02-23 01:37:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidChangePhysicalScreen
|
|
|
|
{
|
2017-01-27 20:41:40 +01:00
|
|
|
vo_cocoa_update_displaylink(self.vout);
|
2017-01-28 16:29:22 +01:00
|
|
|
flag_events(self.vout, VO_EVENT_WIN_STATE);
|
2016-06-17 04:02:51 +02:00
|
|
|
}
|
|
|
|
|
2017-07-27 21:28:33 +02:00
|
|
|
- (void)windowDidEnterFullScreen
|
2016-12-04 22:52:14 +01:00
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = self.vout->cocoa;
|
|
|
|
s->fullscreen = 1;
|
2017-05-19 17:44:20 +02:00
|
|
|
vo_cocoa_anim_unlock(self.vout);
|
2016-12-04 22:52:14 +01:00
|
|
|
}
|
|
|
|
|
2017-07-27 21:28:33 +02:00
|
|
|
- (void)windowDidExitFullScreen
|
2016-12-04 22:52:14 +01:00
|
|
|
{
|
|
|
|
struct vo_cocoa_state *s = self.vout->cocoa;
|
|
|
|
s->fullscreen = 0;
|
2017-05-19 17:44:20 +02:00
|
|
|
vo_cocoa_anim_unlock(self.vout);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowWillEnterFullScreen:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
vo_cocoa_anim_lock(self.vout);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowWillExitFullScreen:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
vo_cocoa_anim_lock(self.vout);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidFailToEnterFullScreen:(NSWindow *)window
|
|
|
|
{
|
|
|
|
vo_cocoa_anim_unlock(self.vout);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidFailToExitFullScreen:(NSWindow *)window
|
|
|
|
{
|
|
|
|
vo_cocoa_anim_unlock(self.vout);
|
2016-12-04 22:52:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowWillStartLiveResize:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
// Make vo.c not do video timing, which would slow down resizing.
|
|
|
|
vo_event(self.vout, VO_EVENT_LIVE_RESIZING);
|
2017-02-13 18:37:51 +01:00
|
|
|
vo_cocoa_stop_displaylink(self.vout->cocoa);
|
2016-12-04 22:52:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidEndLiveResize:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
vo_query_and_reset_events(self.vout, VO_EVENT_LIVE_RESIZING);
|
2017-02-13 18:37:51 +01:00
|
|
|
vo_cocoa_start_displaylink(self.vout->cocoa);
|
2016-12-04 22:52:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)didChangeWindowedScreenProfile:(NSNotification *)notification
|
2014-02-25 00:04:30 +01:00
|
|
|
{
|
2017-01-31 16:45:15 +01:00
|
|
|
vo_cocoa_update_screen_info(self.vout);
|
cocoa: redo synchronization
Before this change, Cocoa state was accessed from both the VO and the
Cocoa main thread. This was probably not a good idea. There was some
locking as well as implicit synchronization using the dispatch
mechanism, but it wasn't watertight.
Change this completely. Now Cocoa things are always accessed from the
main thread only. The old mutex falls away, as well as the
vo_cocoa_set_current_context() function, which implicitly used the lock
to coordinate VO accesses. With the new code, the VO thread generally
has to wait for the main thread, while the main thread never waits for
the VO and rarely accesses it. Fortunately, this is rather straight
forward, and most of this is achieved by making vo_cocoa_control() run
on the main thread. The logic of the code does generally not change.
Some aspects are trickier. Apparently we can't access the
NSOpenGLContext from the VO thread, because this object is not thread-
safe. We use some CGLContextObj functions instead, such as for making
the context current and swapping the buffers.
2015-05-13 22:00:34 +02:00
|
|
|
flag_events(self.vout, VO_EVENT_ICC_PROFILE_CHANGED);
|
2014-02-25 00:04:30 +01:00
|
|
|
}
|
2015-03-08 11:00:29 +01:00
|
|
|
|
2015-03-08 15:19:17 +01:00
|
|
|
- (void)windowDidResignKey:(NSNotification *)notification
|
|
|
|
{
|
2017-02-18 00:46:06 +01:00
|
|
|
vo_cocoa_update_cursor_visibility(self.vout, true);
|
2015-03-08 15:19:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidBecomeKey:(NSNotification *)notification
|
|
|
|
{
|
2017-02-18 00:46:06 +01:00
|
|
|
vo_cocoa_update_cursor_visibility(self.vout, false);
|
2015-03-08 15:19:17 +01:00
|
|
|
}
|
|
|
|
|
2017-02-20 17:25:21 +01:00
|
|
|
- (void)windowWillMove:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
self.vout->cocoa->window_is_dragged = true;
|
|
|
|
}
|
|
|
|
|
2013-07-21 10:33:18 +02:00
|
|
|
@end
|