summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFRAU KOUJIRO <frau@doushio.com>2014-08-11 01:49:50 -0700
committerwm4 <wm4@nowhere>2014-08-12 23:40:58 +0200
commit060ba226e66859d542e43a72320797cb117e7ab1 (patch)
tree18df89c93409c64b40f40cc8b043185b4fac4cd0
parentdcf4ee905b1cc278f8436aa1d2d79f118eb66c9d (diff)
downloadmpv-060ba226e66859d542e43a72320797cb117e7ab1.tar.bz2
mpv-060ba226e66859d542e43a72320797cb117e7ab1.tar.xz
docs: cocoa example uses wakeup callback API
Also, imitate the qt example somewhat.
-rw-r--r--DOCS/client_api_examples/cocoabasic.m76
1 files changed, 54 insertions, 22 deletions
diff --git a/DOCS/client_api_examples/cocoabasic.m b/DOCS/client_api_examples/cocoabasic.m
index e526846db4..d7f2dc125f 100644
--- a/DOCS/client_api_examples/cocoabasic.m
+++ b/DOCS/client_api_examples/cocoabasic.m
@@ -11,9 +11,12 @@
@interface AppDelegate : NSObject <NSApplicationDelegate>
{
mpv_handle *mpv;
+ dispatch_queue_t queue;
}
@end
+static void wakeup(void *);
+
@implementation AppDelegate
- (void) applicationDidFinishLaunching:(NSNotification *)notification {
@@ -25,49 +28,78 @@
}
NSString *filename = args[1];
- // Run MPV loop on its own queue
- dispatch_async(dispatch_queue_create("mpv", DISPATCH_QUEUE_SERIAL), ^{
+ // Deal with MPV in the background.
+ queue = dispatch_queue_create("mpv", DISPATCH_QUEUE_SERIAL);
+ dispatch_async(queue, ^{
- // Set up MPV
mpv = mpv_create();
if (!mpv) {
printf("failed creating context\n");
exit(1);
}
+
+ // Maybe set some options here, like default key bindings.
+ // NOTE: Interaction with the window seems to be broken for now.
+ check_error(mpv_set_option_string(mpv, "input-default-bindings", "yes"));
+
check_error(mpv_initialize(mpv));
+ // Register to be woken up whenever mpv generates new events.
+ mpv_set_wakeup_callback(mpv, wakeup, (__bridge void *) self);
+
+ // Load the indicated file
const char *cmd[] = {"loadfile", filename.UTF8String, NULL};
check_error(mpv_command(mpv, cmd));
+ });
+}
+
+- (void) handleEvent:(mpv_event *)event
+{
+ switch (event->event_id) {
+ case MPV_EVENT_SHUTDOWN:
+ // Clean up and shut down.
+ mpv_terminate_destroy(mpv);
+ mpv = NULL;
+ dispatch_async(dispatch_get_main_queue(), ^{
+ [[NSApplication sharedApplication] terminate:nil];
+ });
+ break;
+
+ default:
+ printf("event: %s\n", mpv_event_name(event->event_id));
+ }
+}
- // Listen for events
- mpv_event *event;
- do {
- event = mpv_wait_event(mpv, -1);
- switch (event->event_id) {
- case MPV_EVENT_NONE:
- break;
- default:
- printf("event: %s\n", mpv_event_name(event->event_id));
- }
- } while (event->event_id != MPV_EVENT_SHUTDOWN);
-
- // Clean up and shut down
- mpv_terminate_destroy(mpv);
- mpv = nil;
- dispatch_async(dispatch_get_main_queue(), ^{
- [[NSApplication sharedApplication] terminate:nil];
- });
+- (void) readEvents
+{
+ dispatch_async(queue, ^{
+ while (mpv) {
+ mpv_event *event = mpv_wait_event(mpv, 0);
+ if (event->event_id == MPV_EVENT_NONE)
+ break;
+ [self handleEvent:event];
+ }
});
}
+static void wakeup(void *context) {
+ AppDelegate *a = (__bridge AppDelegate *) context;
+ [a readEvents];
+}
+
// Ostensibly, mpv's window would be hooked up to this.
- (BOOL) windowShouldClose:(id)sender
{
+ [self shutdown];
+ return YES;
+}
+
+- (void) shutdown
+{
if (mpv) {
const char *args[] = {"quit", NULL};
mpv_command(mpv, args);
}
- return YES;
}
@end