maincontext: Iterate the NSApplication runloop on OSX
@sdroege
Submitted by Sebastian Dröge Link to original bug (#741450)
Description
Created attachment 292619 WIP patch
See summary, and also see bug #704374 (comment 18 and following especially) for some background.
We would like to have something like this in GMainContext to make sure that on OSX a maincontext running on the main thread is also iterating the runloop on that thread.
With this applications and libraries
- can use the GCD and the main queue unconditionally
- can use other OSX APIs without worrying that nothing is iterating the main thread runloop
In GStreamer this was the source of many crashes and deadlocks. We want to be able to always e.g. create a NSWindow on OSX, independent of the application being a GLib application that uses GDK, or one that does not use GDK, or a native OSX application. There are a few hacks in place for that currently[0][1] but the only reliable solution for this is by adding things to GMainContext. We also can't do the GDK hack because there is not necessarily something iterating the default main context (native applications), but we have no way of knowing if that is the case or not.
In GDK this would allow to get rid of the custom main context handling, which also does not work properly according to bug #704374.
Attached patch does this, and is based on the GDK code. For making use of this in GDK a bit more is needed though: GDK needs to be able to intercept native events before we send them off to the NSApp. The patch is currently WIP and still has lots of debug printfs everywhere, I just wanted to get it out there to get some feedback on the general idea already. It works for the GStreamer use cases and allows to get rid of the code in [0][1].
For this I would propose to add some kind of function that can be set on the main context for intercepting the events. Unfortunately this API would be OSX specific, and would need some kind of glib-osx (like glib-unix currently). If anybody has better ideas please let me know :)
[0] http://cgit.freedesktop.org/gstreamer/gst-plugins-bad/tree/gst-libs/gst/gl/cocoa/gstglcontext_cocoa.m#n46 [1] http://cgit.freedesktop.org/gstreamer/gst-plugins-good/tree/sys/osxvideo/osxvideosink.m#n74
Patch 292619, "WIP patch":
0001-WIP-implementation-of-Cocoa-event-loop-integration-i.patch