mirror of https://github.com/mpv-player/mpv
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:
parent
8b6b84f36b
commit
c2eca2e4b7
|
@ -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));
|
||||||
|
|
||||||
|
@ -107,17 +112,27 @@ static void wakeup(void *);
|
||||||
- (void) handleEvent:(mpv_event *)event
|
- (void) handleEvent:(mpv_event *)event
|
||||||
{
|
{
|
||||||
switch (event->event_id) {
|
switch (event->event_id) {
|
||||||
case MPV_EVENT_SHUTDOWN:
|
case MPV_EVENT_SHUTDOWN:
|
||||||
// Clean up and shut down.
|
// Clean up and shut down.
|
||||||
mpv_terminate_destroy(mpv);
|
mpv_terminate_destroy(mpv);
|
||||||
mpv = NULL;
|
mpv = NULL;
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
[[NSApplication sharedApplication] terminate:nil];
|
[[NSApplication sharedApplication] terminate:nil];
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
case MPV_EVENT_LOG_MESSAGE: {
|
||||||
printf("event: %s\n", mpv_event_name(event->event_id));
|
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:
|
||||||
|
printf("event: %s\n", mpv_event_name(event->event_id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,6 +43,7 @@
|
||||||
if (self) {
|
if (self) {
|
||||||
[self registerForDraggedTypes:@[NSFilenamesPboardType,
|
[self registerForDraggedTypes:@[NSFilenamesPboardType,
|
||||||
NSURLPboardType]];
|
NSURLPboardType]];
|
||||||
|
[self setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
[s->window release];
|
||||||
if ([s->view isInFullScreenMode])
|
|
||||||
[[s->view window] release];
|
|
||||||
|
|
||||||
[s->window release];
|
|
||||||
s->window = nil;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -307,22 +301,27 @@ 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);
|
||||||
MpvEventsView *view = [[MpvEventsView alloc] initWithFrame:contentRect];
|
parent = [s->window contentView];
|
||||||
[view autorelease];
|
|
||||||
view.adapter = adapter;
|
|
||||||
s->view = view;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MpvEventsView *view = [[MpvEventsView alloc] initWithFrame:[parent bounds]];
|
||||||
|
view.adapter = adapter;
|
||||||
|
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));
|
||||||
cocoa_register_menu_item_action(MPM_N_SIZE, @selector(normalSize));
|
cocoa_register_menu_item_action(MPM_N_SIZE, @selector(normalSize));
|
||||||
|
@ -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,23 +712,28 @@ void *vo_cocoa_cgl_pixel_format(struct vo *vo)
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)signalMouseMovement:(NSPoint)point {
|
- (void)signalMouseMovement:(NSPoint)point {
|
||||||
mp_input_set_mouse_pos(self.vout->input_ctx, point.x, point.y);
|
if (mp_input_mouse_enabled(self.vout->input_ctx)) {
|
||||||
[self recalcMovableByWindowBackground:point];
|
mp_input_set_mouse_pos(self.vout->input_ctx, point.x, point.y);
|
||||||
|
[self recalcMovableByWindowBackground:point];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)putKeyEvent:(NSEvent*)event
|
- (void)putKeyEvent:(NSEvent*)event
|
||||||
{
|
{
|
||||||
cocoa_put_key_event(event);
|
if (mp_input_vo_keyboard_enabled(self.vout->input_ctx))
|
||||||
|
cocoa_put_key_event(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)putKey:(int)mpkey withModifiers:(int)modifiers
|
- (void)putKey:(int)mpkey withModifiers:(int)modifiers
|
||||||
{
|
{
|
||||||
cocoa_put_key_with_modifiers(mpkey, modifiers);
|
if (mp_input_vo_keyboard_enabled(self.vout->input_ctx))
|
||||||
|
cocoa_put_key_with_modifiers(mpkey, modifiers);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)putAxis:(int)mpkey delta:(float)delta;
|
- (void)putAxis:(int)mpkey delta:(float)delta;
|
||||||
{
|
{
|
||||||
mp_input_put_axis(self.vout->input_ctx, mpkey, delta);
|
if (mp_input_mouse_enabled(self.vout->input_ctx))
|
||||||
|
mp_input_put_axis(self.vout->input_ctx, mpkey, delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)putCommand:(char*)cmd
|
- (void)putCommand:(char*)cmd
|
||||||
|
|
Loading…
Reference in New Issue