[gtk/wip/chergert/quartz4u] simplify to single event source



commit 22ff3d0230b087576aad87f4811a505a453166a8
Author: Christian Hergert <chergert redhat com>
Date:   Mon Apr 27 13:33:05 2020 -0700

    simplify to single event source

 gdk/macos/gdkmacosdisplay.c             | 82 +++++-------------------------
 gdk/macos/gdkmacoseventsource-private.h |  2 +-
 gdk/macos/gdkmacoseventsource.c         | 88 +++++++++++++++++++++------------
 3 files changed, 70 insertions(+), 102 deletions(-)
---
diff --git a/gdk/macos/gdkmacosdisplay.c b/gdk/macos/gdkmacosdisplay.c
index beab658a9c..e0ae8b161f 100644
--- a/gdk/macos/gdkmacosdisplay.c
+++ b/gdk/macos/gdkmacosdisplay.c
@@ -20,29 +20,16 @@
 #include "config.h"
 
 #include <AppKit/AppKit.h>
-#include <fcntl.h>
 #include <gdk/gdk.h>
-#include <unistd.h>
-
-#include <dispatch/dispatch.h>
-#include <errno.h>
-#include <mach/mach.h>
-#include <mach/port.h>
-#include <sys/event.h>
-#include <sys/time.h>
 
 #include "gdkdisplayprivate.h"
+#include "gdkmacoseventsource-private.h"
 #include "gdkmacosdisplay-private.h"
 #include "gdkmacoskeymap-private.h"
 #include "gdkmacosmonitor-private.h"
 #include "gdkmacossurface-private.h"
 #include "gdkmacosutils-private.h"
 
-/* See https://daurnimator.com/post/147024385399/using-your-own-main-loop-on-osx
- * for more information on integrating Cocoa's CFRunLoop using an FD.
- */
-extern mach_port_t _dispatch_get_main_queue_port_4CF (void);
-
 /**
  * SECTION:macos_interaction
  * @Short_description: macOS backend-specific functions
@@ -79,13 +66,11 @@ extern mach_port_t _dispatch_get_main_queue_port_4CF (void);
 
 struct _GdkMacosDisplay
 {
-  GdkDisplay      parent_instance;
+  GdkDisplay           parent_instance;
 
-  gchar          *name;
-  GPtrArray      *monitors;
-  GdkMacosKeymap *keymap;
-
-  int             fd;
+  gchar               *name;
+  GPtrArray           *monitors;
+  GdkMacosKeymap      *keymap;
 };
 
 struct _GdkMacosDisplayClass
@@ -95,6 +80,8 @@ struct _GdkMacosDisplayClass
 
 G_DEFINE_TYPE (GdkMacosDisplay, gdk_macos_display, GDK_TYPE_DISPLAY)
 
+static GSource *event_source;
+
 static gboolean
 gdk_macos_display_get_setting (GdkDisplay  *display,
                                const gchar *setting,
@@ -310,12 +297,6 @@ gdk_macos_display_finalize (GObject *object)
   g_clear_pointer (&self->monitors, g_ptr_array_unref);
   g_clear_pointer (&self->name, g_free);
 
-  if (self->fd != -1)
-    {
-      close (self->fd);
-      self->fd = -1;
-    }
-
   G_OBJECT_CLASS (gdk_macos_display_parent_class)->finalize (object);
 }
 
@@ -351,7 +332,6 @@ static void
 gdk_macos_display_init (GdkMacosDisplay *self)
 {
   self->monitors = g_ptr_array_new_with_free_func (g_object_unref);
-  self->fd = -1;
 }
 
 GdkDisplay *
@@ -375,49 +355,13 @@ _gdk_macos_display_open (const gchar *display_name)
 
   gdk_macos_display_load_monitors (self);
 
-  gdk_display_emit_opened (GDK_DISPLAY (self));
-
-  return GDK_DISPLAY (self);
-}
-
-int
-_gdk_macos_display_get_fd (GdkMacosDisplay *self)
-{
-  g_return_val_if_fail (GDK_IS_MACOS_DISPLAY (self), -1);
-
-  if (self->fd == -1)
+  if (event_source == NULL)
     {
-      int fd = kqueue ();
-
-      if (fd != -1)
-        {
-          mach_port_t port;
-          mach_port_t portset;
-
-          fcntl (fd, F_SETFD, FD_CLOEXEC);
-          port = _dispatch_get_main_queue_port_4CF ();
-
-          if (KERN_SUCCESS == mach_port_allocate (mach_task_self (),
-                                                  MACH_PORT_RIGHT_PORT_SET,
-                                                  &portset))
-            {
-              struct kevent64_s event;
-
-              EV_SET64 (&event, portset, EVFILT_MACHPORT, EV_ADD|EV_CLEAR, MACH_RCV_MSG, 0, 0, 0, 0);
-
-              if (kevent64 (fd, &event, 1, NULL, 0, 0, &(struct timespec){0,0}) != 0)
-                {
-                  if (KERN_SUCCESS == mach_port_insert_member (mach_task_self (), port, portset))
-                    {
-                      self->fd = fd;
-                      return fd;
-                    }
-                }
-            }
-
-          close (fd);
-        }
+      event_source = _gdk_macos_event_source_new ();
+      g_source_attach (event_source, NULL);
     }
 
-  return self->fd;
+  gdk_display_emit_opened (GDK_DISPLAY (self));
+
+  return GDK_DISPLAY (self);
 }
diff --git a/gdk/macos/gdkmacoseventsource-private.h b/gdk/macos/gdkmacoseventsource-private.h
index 60edcfdfa6..63811ecd38 100644
--- a/gdk/macos/gdkmacoseventsource-private.h
+++ b/gdk/macos/gdkmacoseventsource-private.h
@@ -24,7 +24,7 @@
 
 G_BEGIN_DECLS
 
-GSource *_gdk_macos_event_source_new (GdkMacosDisplay *display);
+GSource *_gdk_macos_event_source_new (void);
 
 G_END_DECLS
 
diff --git a/gdk/macos/gdkmacoseventsource.c b/gdk/macos/gdkmacoseventsource.c
index 3f500ee55a..b5ded48ae2 100644
--- a/gdk/macos/gdkmacoseventsource.c
+++ b/gdk/macos/gdkmacoseventsource.c
@@ -19,8 +19,15 @@
 
 #include "config.h"
 
+#include <errno.h>
+#include <fcntl.h>
 #include <mach/mach.h>
+#include <mach/mach.h>
+#include <mach/port.h>
 #include <mach/port.h>
+#include <sys/event.h>
+#include <sys/time.h>
+#include <unistd.h>
 
 #include "gdkinternals.h"
 
@@ -30,23 +37,19 @@
 /* See https://daurnimator.com/post/147024385399/using-your-own-main-loop-on-osx
  * for more information on integrating Cocoa's CFRunLoop using an FD.
  */
-extern void _dispatch_main_queue_callback_4CF (void);
+extern mach_port_t _dispatch_get_main_queue_port_4CF (void);
+extern void        _dispatch_main_queue_callback_4CF (void);
 
 typedef struct _GdkMacosEventSource
 {
-  GSource          source;
-  GPollFD          pfd;
-  GdkMacosDisplay *display;
+  GSource source;
+  GPollFD pfd;
 } GdkMacosEventSource;
 
 static gboolean
 gdk_macos_event_source_check (GSource *base)
 {
   GdkMacosEventSource *source = (GdkMacosEventSource *)base;
-
-  g_assert (source != NULL);
-  g_assert (GDK_IS_MACOS_DISPLAY (source->display));
-
   return (source->pfd.revents & G_IO_IN) != 0;
 }
 
@@ -58,53 +61,74 @@ gdk_macos_event_source_dispatch (GSource     *base,
   GdkMacosEventSource *source = (GdkMacosEventSource *)base;
 
   g_assert (source != NULL);
-  g_assert (GDK_IS_MACOS_DISPLAY (source->display));
 
+  source->pfd.revents = 0;
   _dispatch_main_queue_callback_4CF ();
 
   return G_SOURCE_CONTINUE;
 }
 
-static void
-gdk_macos_event_source_finalize (GSource *base)
-{
-  GdkMacosEventSource *source = (GdkMacosEventSource *)base;
-  source->display = NULL;
-}
-
 static GSourceFuncs macos_event_source_funcs = {
   .prepare = NULL,
   .check = gdk_macos_event_source_check,
   .dispatch = gdk_macos_event_source_dispatch,
-  .finalize = gdk_macos_event_source_finalize,
+  .finalize = NULL,
 };
 
+static int
+gdk_macos_event_source_get_fd (void)
+{
+  int fd = kqueue ();
+
+  if (fd != -1)
+    {
+      mach_port_t port;
+      mach_port_t portset;
+
+      fcntl (fd, F_SETFD, FD_CLOEXEC);
+      port = _dispatch_get_main_queue_port_4CF ();
+
+      if (KERN_SUCCESS == mach_port_allocate (mach_task_self (),
+                                              MACH_PORT_RIGHT_PORT_SET,
+                                              &portset))
+        {
+          struct kevent64_s event;
+
+          EV_SET64 (&event, portset, EVFILT_MACHPORT, EV_ADD|EV_CLEAR, MACH_RCV_MSG, 0, 0, 0, 0);
+
+          if (kevent64 (fd, &event, 1, NULL, 0, 0, &(struct timespec){0,0}) == 0)
+            {
+              if (KERN_SUCCESS == mach_port_insert_member (mach_task_self (), port, portset))
+                return fd;
+            }
+        }
+
+      close (fd);
+    }
+
+  return -1;
+}
+
 GSource *
-_gdk_macos_event_source_new (GdkMacosDisplay *display)
+_gdk_macos_event_source_new (void)
 {
   GdkMacosEventSource *macos_source;
   GSource *source;
-  gchar *name;
+  int fd;
 
-  g_return_val_if_fail (GDK_IS_MACOS_DISPLAY (display), NULL);
+  if ((fd = gdk_macos_event_source_get_fd ()) == -1)
+    return NULL;
 
   source = g_source_new (&macos_event_source_funcs, sizeof (GdkMacosEventSource));
+  g_source_set_name (source, "GDK macOS Event Source");
+  g_source_set_priority (source, GDK_PRIORITY_EVENTS);
+  g_source_set_can_recurse (source, TRUE);
 
   macos_source = (GdkMacosEventSource *)source;
-  macos_source->display = display;
-  macos_source->pfd.fd = _gdk_macos_display_get_fd (display);
+  macos_source->pfd.fd = fd;
   macos_source->pfd.events = G_IO_IN;
+  macos_source->pfd.revents = 0;
   g_source_add_poll (source, &macos_source->pfd);
 
-  name = g_strdup_printf ("GDK macOS Event Source (%s)",
-                          gdk_display_get_name (GDK_DISPLAY (display)));
-
-  g_source_set_name (source, name);
-  g_source_set_priority (source, GDK_PRIORITY_EVENTS);
-  g_source_set_can_recurse (source, TRUE);
-  g_source_attach (source, NULL);
-
-  g_free (name);
-
   return source;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]