libmpv/cocoa: allow clients to use mpv event system

This allows mpv's view to take key and send events to mpv's core.
To set key status correctly, clients must call -[NSWindow selectNextKeyView:]
during reconfig on the main thread. All is 'documented' in the cocoabasic
example.

If someone knows a better way to handle giving key to the embedded view,
let me know!
This commit is contained in:
Stefano Pigozzi 2014-10-12 00:17:48 +02:00
parent 8b6b84f36b
commit c2eca2e4b7
3 changed files with 53 additions and 35 deletions

View File

@ -45,6 +45,7 @@ static void wakeup(void *);
defer:NO]; defer:NO];
[self->w setTitle:@"cocoabasic example"]; [self->w setTitle:@"cocoabasic example"];
[self->w makeMainWindow];
[self->w makeKeyAndOrderFront:nil]; [self->w makeKeyAndOrderFront:nil];
[NSApp activateIgnoringOtherApps:YES]; [NSApp activateIgnoringOtherApps:YES];
} }
@ -92,6 +93,10 @@ static void wakeup(void *);
// for testing! // for testing!
check_error(mpv_set_option_string(mpv, "input-media-keys", "yes")); check_error(mpv_set_option_string(mpv, "input-media-keys", "yes"));
check_error(mpv_set_option_string(mpv, "input-vo-keyboard", "yes"));
// request important errors
check_error(mpv_request_log_messages(mpv, "warn"));
check_error(mpv_initialize(mpv)); check_error(mpv_initialize(mpv));
@ -116,6 +121,16 @@ static void wakeup(void *);
}); });
break; break;
case MPV_EVENT_LOG_MESSAGE: {
struct mpv_event_log_message *msg = (struct mpv_event_log_message *)event->data;
printf("[%s] %s: %s", msg->prefix, msg->level, msg->text);
}
case MPV_EVENT_VIDEO_RECONFIG:
dispatch_async(dispatch_get_main_queue(), ^{
[self->w selectNextKeyView:nil];
});
default: default:
printf("event: %s\n", mpv_event_name(event->event_id)); printf("event: %s\n", mpv_event_name(event->event_id));
} }

View File

@ -43,6 +43,7 @@
if (self) { if (self) {
[self registerForDraggedTypes:@[NSFilenamesPboardType, [self registerForDraggedTypes:@[NSFilenamesPboardType,
NSURLPboardType]]; NSURLPboardType]];
[self setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
} }
return self; return self;
} }

View File

@ -183,14 +183,8 @@ void vo_cocoa_uninit(struct vo *vo)
cocoa_rm_fs_screen_profile_observer(vo); cocoa_rm_fs_screen_profile_observer(vo);
[s->video release]; [s->video release];
[s->view release];
if (!s->embedded) {
if ([s->view isInFullScreenMode])
[[s->view window] release];
[s->window release]; [s->window release];
s->window = nil;
}
}); });
} }
@ -307,21 +301,26 @@ static void create_ui(struct vo *vo, struct mp_rect *win, int geo_flags)
struct mp_vo_opts *opts = vo->opts; struct mp_vo_opts *opts = vo->opts;
MpvCocoaAdapter *adapter = [[[MpvCocoaAdapter alloc] init] autorelease]; 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; adapter.vout = vo;
NSView *parent;
if (s->embedded) { if (s->embedded) {
s->view = (NSView *) (intptr_t) opts->WinID; parent = (NSView *) (intptr_t) opts->WinID;
} else { } else {
s->window = create_window(contentRect, s->current_screen, const NSRect wr =
opts->border, adapter); NSMakeRect(win->x0, win->y0, win->x1 - win->x0, win->y1 - win->y0);
s->window = create_window(wr, s->current_screen, opts->border, adapter);
parent = [s->window contentView];
}
MpvEventsView *view = [[MpvEventsView alloc] initWithFrame:contentRect]; MpvEventsView *view = [[MpvEventsView alloc] initWithFrame:[parent bounds]];
[view autorelease];
view.adapter = adapter; view.adapter = adapter;
s->view = view; s->view = view;
} [parent addSubview:s->view];
// insert ourselves as the next key view so that clients can give key
// focus to the mpv view by calling -[NSWindow selectNextKeyView:]
[parent setNextKeyView:s->view];
#if HAVE_COCOA_APPLICATION #if HAVE_COCOA_APPLICATION
cocoa_register_menu_item_action(MPM_H_SIZE, @selector(halfSize)); cocoa_register_menu_item_action(MPM_H_SIZE, @selector(halfSize));
@ -335,8 +334,6 @@ static void create_ui(struct vo *vo, struct mp_rect *win, int geo_flags)
[s->video setWantsBestResolutionOpenGLSurface:YES]; [s->video setWantsBestResolutionOpenGLSurface:YES];
[s->view addSubview:s->video]; [s->view addSubview:s->video];
[s->view setAutoresizesSubviews:YES];
[s->window setContentView:s->view];
[s->gl_ctx setView:s->video]; [s->gl_ctx setView:s->video];
s->video.adapter = adapter; s->video.adapter = adapter;
@ -715,22 +712,27 @@ void *vo_cocoa_cgl_pixel_format(struct vo *vo)
} }
- (void)signalMouseMovement:(NSPoint)point { - (void)signalMouseMovement:(NSPoint)point {
if (mp_input_mouse_enabled(self.vout->input_ctx)) {
mp_input_set_mouse_pos(self.vout->input_ctx, point.x, point.y); mp_input_set_mouse_pos(self.vout->input_ctx, point.x, point.y);
[self recalcMovableByWindowBackground:point]; [self recalcMovableByWindowBackground:point];
} }
}
- (void)putKeyEvent:(NSEvent*)event - (void)putKeyEvent:(NSEvent*)event
{ {
if (mp_input_vo_keyboard_enabled(self.vout->input_ctx))
cocoa_put_key_event(event); cocoa_put_key_event(event);
} }
- (void)putKey:(int)mpkey withModifiers:(int)modifiers - (void)putKey:(int)mpkey withModifiers:(int)modifiers
{ {
if (mp_input_vo_keyboard_enabled(self.vout->input_ctx))
cocoa_put_key_with_modifiers(mpkey, modifiers); cocoa_put_key_with_modifiers(mpkey, modifiers);
} }
- (void)putAxis:(int)mpkey delta:(float)delta; - (void)putAxis:(int)mpkey delta:(float)delta;
{ {
if (mp_input_mouse_enabled(self.vout->input_ctx))
mp_input_put_axis(self.vout->input_ctx, mpkey, delta); mp_input_put_axis(self.vout->input_ctx, mpkey, delta);
} }