From 69fa956f5012f92186e123af37b5bbff8554bbdc Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 8 Oct 2014 19:23:47 +0200 Subject: cocoa: allow to embed into an arbitrary NSView Basically add if guards on all the problematic features. I'm still thinking about a better way to handle this, but for the time being, this will do. --- DOCS/client_api_examples/cocoabasic.m | 2 +- video/out/cocoa/events_view.h | 1 - video/out/cocoa/events_view.m | 5 ----- video/out/cocoa/video_view.h | 1 + video/out/cocoa/video_view.m | 5 +++++ video/out/cocoa_common.m | 39 ++++++++++++++++++++++++----------- 6 files changed, 34 insertions(+), 19 deletions(-) diff --git a/DOCS/client_api_examples/cocoabasic.m b/DOCS/client_api_examples/cocoabasic.m index 5c1a89bc2e..3324f13845 100644 --- a/DOCS/client_api_examples/cocoabasic.m +++ b/DOCS/client_api_examples/cocoabasic.m @@ -82,7 +82,7 @@ static void wakeup(void *); } #if EMBED_VIEW - int64_t wid = (intptr_t) self->w; + int64_t wid = (intptr_t) [self->w contentView]; check_error(mpv_set_option(mpv, "wid", MPV_FORMAT_INT64, &wid)); #endif diff --git a/video/out/cocoa/events_view.h b/video/out/cocoa/events_view.h index 3dc0e0306e..ff2500a7bc 100644 --- a/video/out/cocoa/events_view.h +++ b/video/out/cocoa/events_view.h @@ -21,6 +21,5 @@ @interface MpvEventsView : NSView @property(nonatomic, retain) MpvCocoaAdapter *adapter; - (void)setFullScreen:(BOOL)willBeFullscreen; -- (NSRect)frameInPixels; - (BOOL)canHideCursor; @end diff --git a/video/out/cocoa/events_view.m b/video/out/cocoa/events_view.m index 91b00fcb0b..744c3f27c7 100644 --- a/video/out/cocoa/events_view.m +++ b/video/out/cocoa/events_view.m @@ -130,11 +130,6 @@ - (BOOL)becomeFirstResponder { return YES; } - (BOOL)resignFirstResponder { return YES; } -- (NSRect)frameInPixels -{ - return [self convertRectToBacking:[self frame]]; -} - - (BOOL)canHideCursor { return !self.hasMouseDown && [self containsMouseLocation]; diff --git a/video/out/cocoa/video_view.h b/video/out/cocoa/video_view.h index d31d1e014e..3935a9b0af 100644 --- a/video/out/cocoa/video_view.h +++ b/video/out/cocoa/video_view.h @@ -20,4 +20,5 @@ @interface MpvVideoView : NSView @property(nonatomic, retain) MpvCocoaAdapter *adapter; +- (NSRect)frameInPixels; @end diff --git a/video/out/cocoa/video_view.m b/video/out/cocoa/video_view.m index 851954c167..07db876534 100644 --- a/video/out/cocoa/video_view.m +++ b/video/out/cocoa/video_view.m @@ -35,4 +35,9 @@ [super setFrameSize:size]; [self.adapter setNeedsResize]; } + +- (NSRect)frameInPixels +{ + return [self convertRectToBacking:[self frame]]; +} @end diff --git a/video/out/cocoa_common.m b/video/out/cocoa_common.m index 0dab97826c..7eabad53ba 100644 --- a/video/out/cocoa_common.m +++ b/video/out/cocoa_common.m @@ -58,7 +58,7 @@ static void cocoa_rm_fs_screen_profile_observer(struct vo *vo); struct vo_cocoa_state { NSWindow *window; - MpvEventsView *view; + NSView *view; MpvVideoView *video; NSOpenGLContext *gl_ctx; @@ -155,17 +155,24 @@ int vo_cocoa_init(struct vo *vo) return 1; } -static void vo_cocoa_set_cursor_visibility(struct vo *vo, bool *visible) +static int vo_cocoa_set_cursor_visibility(struct vo *vo, bool *visible) { struct vo_cocoa_state *s = vo->cocoa; + if (s->embedded) + return VO_NOTIMPL; + + MpvEventsView *v = (MpvEventsView *) s->view; + if (*visible) { CGDisplayShowCursor(kCGDirectMainDisplay); - } else if ([s->view canHideCursor]) { + } else if ([v canHideCursor]) { CGDisplayHideCursor(kCGDirectMainDisplay); } else { *visible = true; } + + return VO_TRUE; } void vo_cocoa_uninit(struct vo *vo) @@ -225,6 +232,9 @@ static void vo_cocoa_update_screen_info(struct vo *vo, struct mp_rect *out_rc) { struct vo_cocoa_state *s = vo->cocoa; + if (s->embedded) + return; + vo_cocoa_update_screens_pointers(vo); if (out_rc) { @@ -236,7 +246,7 @@ static void vo_cocoa_update_screen_info(struct vo *vo, struct mp_rect *out_rc) static void resize_window(struct vo *vo) { struct vo_cocoa_state *s = vo->cocoa; - NSRect frame = [s->view frameInPixels]; + NSRect frame = [s->video frameInPixels]; vo->dwidth = frame.size.width; vo->dheight = frame.size.height; [s->gl_ctx update]; @@ -301,14 +311,19 @@ static void create_ui(struct vo *vo, struct mp_rect *win, int geo_flags) MpvCocoaAdapter *adapter = [[[MpvCocoaAdapter alloc] init] autorelease]; const NSRect contentRect = NSMakeRect(win->x0, win->y0, win->x1 - win->x0, win->y1 - win->y0); + adapter.vout = vo; if (s->embedded) { - s->window = (NSWindow *) (intptr_t) opts->WinID; + s->view = (NSView *) (intptr_t) opts->WinID; } else { s->window = create_window(contentRect, s->current_screen, opts->border, adapter); + + MpvEventsView *view = [[MpvEventsView alloc] initWithFrame:contentRect]; + [view autorelease]; + view.adapter = adapter; + s->view = view; } - s->view = [[[MpvEventsView alloc] initWithFrame:contentRect] autorelease]; #if HAVE_COCOA_APPLICATION cocoa_register_menu_item_action(MPM_H_SIZE, @selector(halfSize)); @@ -326,8 +341,6 @@ static void create_ui(struct vo *vo, struct mp_rect *win, int geo_flags) [s->window setContentView:s->view]; [s->gl_ctx setView:s->video]; - adapter.vout = vo; - s->view.adapter = adapter; s->video.adapter = adapter; if (!s->embedded) { @@ -414,7 +427,7 @@ int vo_cocoa_config_window(struct vo *vo, uint32_t flags, void *gl_ctx) s->old_dwidth = width; s->old_dheight = height; - if (!(flags & VOFLAG_HIDDEN) && !s->window) { + if (!(flags & VOFLAG_HIDDEN) && !s->view) { create_ui(vo, &geo.win, geo.flags); } @@ -499,10 +512,13 @@ static void vo_cocoa_fullscreen(struct vo *vo) struct vo_cocoa_state *s = vo->cocoa; struct mp_vo_opts *opts = vo->opts; + if (s->embedded) + return; + vo_cocoa_update_screen_info(vo, NULL); draw_changes_after_next_frame(vo); - [s->view setFullScreen:opts->fullscreen]; + [(MpvEventsView *)s->view setFullScreen:opts->fullscreen]; if (s->icc_fs_profile_path != s->icc_wnd_profile_path) s->pending_events = VO_EVENT_ICC_PROFILE_PATH_CHANGED; @@ -646,8 +662,7 @@ int vo_cocoa_control(struct vo *vo, int *events, int request, void *arg) return VO_TRUE; } case VOCTRL_SET_CURSOR_VISIBILITY: - vo_cocoa_set_cursor_visibility(vo, arg); - return VO_TRUE; + return vo_cocoa_set_cursor_visibility(vo, arg); case VOCTRL_UPDATE_WINDOW_TITLE: return cocoa_set_window_title(vo, (const char *) arg); case VOCTRL_RESTORE_SCREENSAVER: -- cgit v1.2.3