mediafile: Turn into an extension point
authorMatthias Clasen <mclasen@redhat.com>
Thu, 1 Mar 2018 20:46:16 +0000 (21:46 +0100)
committerBenjamin Otte <otte@redhat.com>
Sun, 18 Mar 2018 20:01:23 +0000 (21:01 +0100)
This way, we can support external libraries providing implementations of
GtkMediaFile.

We also add a media backend called 'nomedia' that can be enabled to not
compile any support for GtkMediaFile. This is useful when people want to
statically compile GTK into an application that does not use media.
For now, this option is the default.

We also support a new environment variable GTK_MEDIA that allows
selecting the implementation to use.
GTK_MEDIA=help can be used to get info about the available
implementations.

gtk/gtkmain.c
gtk/gtkmediafile.c
gtk/gtkmediafile.h
gtk/gtkmediafileprivate.h [new file with mode: 0644]
gtk/gtknomediafile.c [new file with mode: 0644]
gtk/gtknomediafileprivate.h [new file with mode: 0644]
gtk/meson.build
meson.build
meson_options.txt
modules/media/meson.build [new file with mode: 0644]
modules/meson.build

index 336c9363aea29a5737056656f8c71893d1b6352b..bd0720d5c8bfab300477420eed3cdc27141b72ff 100644 (file)
 #include "gtkdebugupdatesprivate.h"
 #include "gtkdndprivate.h"
 #include "gtkmain.h"
+#include "gtkmediafileprivate.h"
 #include "gtkmenu.h"
 #include "gtkmodulesprivate.h"
 #include "gtkprivate.h"
@@ -632,6 +633,7 @@ default_display_notify_cb (GdkDisplayManager *dm)
   debug_flags[0].display = gdk_display_get_default ();
   gtk_print_backends_init ();
   gtk_im_modules_init ();
+  gtk_media_file_extension_init ();
   _gtk_accessibility_init ();
 }
 
index edc121fe3e7a178a93bf2612c9611a46cf601fb8..e3b0e6275622fa42c136503b2ebe9850a12b8333 100644 (file)
 
 #include "config.h"
 
-#include "gtkmediafile.h"
+#include "gtkmediafileprivate.h"
 
+#include "gtkdebug.h"
 #include "gtkintl.h"
+#include "gtkmodulesprivate.h"
+#include "gtknomediafileprivate.h"
 
 /**
  * SECTION:gtkmediafile
@@ -179,8 +182,64 @@ gtk_media_file_init (GtkMediaFile *self)
 static GType
 gtk_media_file_get_impl_type (void)
 {
-  g_assert_not_reached ();
-  return G_TYPE_INVALID;
+  static GType impl_type = G_TYPE_NONE;
+  const char *extension_name;
+  GIOExtension *e;
+  GIOExtensionPoint *ep;
+
+  if (G_LIKELY (impl_type != G_TYPE_NONE))
+    return impl_type;
+
+  GTK_NOTE (MODULES, g_print ("Looking up MediaFile extension\n"));
+  
+  ep = g_io_extension_point_lookup (GTK_MEDIA_FILE_EXTENSION_POINT_NAME);
+  e = NULL;
+
+  extension_name = g_getenv ("GTK_MEDIA");
+  if (extension_name)
+    {
+      if (g_str_equal (extension_name, "help"))
+        {
+          GList *l;
+
+          g_print ("Supported arguments for GTK_MEDIA environment variable:\n");
+
+          for (l = g_io_extension_point_get_extensions (ep); l; l = l->next)
+            {
+              e = l->data;
+
+              g_print ("%10s - %d\n", g_io_extension_get_name (e), g_io_extension_get_priority (e));
+            }
+
+          e = NULL;
+        }
+      else
+        {
+          e = g_io_extension_point_get_extension_by_name (ep, extension_name);
+          if (e == NULL)
+            {
+              g_warning ("Media extension \"%s\" from GTK_MEDIA environment variable not found.", extension_name);
+            }
+        }
+    }
+
+  if (e == NULL)
+    {
+      GList *l = g_io_extension_point_get_extensions (ep);
+
+      if (l == NULL)
+        {
+          g_error ("GTK was run without any GtkMediaFile extension being present. This must not happen.");
+        }
+
+      e = l->data;
+    }
+
+  impl_type = g_io_extension_get_type (e);
+
+  GTK_NOTE (MODULES, g_print ("Using %s from \"%s\" extension\n", g_type_name (impl_type), g_io_extension_get_name (e)));
+
+  return impl_type;
 }
 
 /**
@@ -524,3 +583,47 @@ gtk_media_file_get_input_stream (GtkMediaFile *self)
 
   return priv->input_stream;
 }
+
+void
+gtk_media_file_extension_init (void)
+{
+  GIOExtensionPoint *ep;
+  GIOModuleScope *scope;
+  char **paths;
+  int i;
+
+  GTK_NOTE (MODULES,
+            g_print ("Registering extension point %s\n", GTK_MEDIA_FILE_EXTENSION_POINT_NAME));
+
+  ep = g_io_extension_point_register (GTK_MEDIA_FILE_EXTENSION_POINT_NAME);
+  g_io_extension_point_set_required_type (ep, GTK_TYPE_MEDIA_FILE);
+
+  g_type_ensure (GTK_TYPE_NO_MEDIA_FILE);
+
+  scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);
+
+  paths = _gtk_get_module_path ("media");
+  for (i = 0; paths[i]; i++)
+    {
+      GTK_NOTE (MODULES,
+                g_print ("Scanning io modules in %s\n", paths[i]));
+      g_io_modules_scan_all_in_directory_with_scope (paths[i], scope);
+    }
+  g_strfreev (paths);
+
+  g_io_module_scope_free (scope);
+
+  if (GTK_DEBUG_CHECK (MODULES))
+    {
+      GList *list, *l;
+
+      list = g_io_extension_point_get_extensions (ep);
+      for (l = list; l; l = l->next)
+        {
+          GIOExtension *ext = l->data;
+          g_print ("extension: %s: type %s\n",
+                   g_io_extension_get_name (ext),
+                   g_type_name (g_io_extension_get_type (ext)));
+        }
+    }
+}
index 20f38399a4322a7920e898201d58a0e04b099b05..daa17d9aa875b90e027c8f62c5797565ce6c34aa 100644 (file)
@@ -28,6 +28,8 @@
 
 G_BEGIN_DECLS
 
+#define GTK_MEDIA_FILE_EXTENSION_POINT_NAME "gtk-media-file"
+
 #define GTK_TYPE_MEDIA_FILE             (gtk_media_file_get_type ())
 
 GDK_AVAILABLE_IN_ALL
diff --git a/gtk/gtkmediafileprivate.h b/gtk/gtkmediafileprivate.h
new file mode 100644 (file)
index 0000000..b328183
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright © 2018 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte@gnome.org>
+ */
+
+#ifndef __GTK_MEDIA_FILE_PRIVATE_H__
+#define __GTK_MEDIA_FILE_PRIVATE_H__
+
+#include "gtkmediafile.h"
+
+
+void            gtk_media_file_extension_init                   (void);
+
+
+#endif /* __GTK_MEDIA_FILE_PRIVATE_H__ */
diff --git a/gtk/gtknomediafile.c b/gtk/gtknomediafile.c
new file mode 100644 (file)
index 0000000..18ac36b
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Copyright © 2018 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte@gnome.org>
+ */
+
+#include "config.h"
+
+#include "gtknomediafileprivate.h"
+
+#include "gtkintl.h"
+
+struct _GtkNoMediaFile
+{
+  GtkMediaFile parent_instance;
+};
+
+struct _GtkNoMediaFileClass
+{
+  GtkMediaFileClass parent_class;
+};
+
+G_DEFINE_TYPE_WITH_CODE (GtkNoMediaFile, gtk_no_media_file, GTK_TYPE_MEDIA_FILE,
+                         g_io_extension_point_implement (GTK_MEDIA_FILE_EXTENSION_POINT_NAME,
+                                                         g_define_type_id,
+                                                         "none",
+                                                         G_MININT);)
+
+static void
+gtk_no_media_file_open (GtkMediaFile *file)
+{
+  gtk_media_stream_error (GTK_MEDIA_STREAM (file),
+                          G_IO_ERROR,
+                          G_IO_ERROR_NOT_SUPPORTED,
+                          _("GTK has been compiled without media support."));
+}
+
+static void
+gtk_no_media_file_class_init (GtkNoMediaFileClass *klass)
+{
+  GtkMediaFileClass *file_class = GTK_MEDIA_FILE_CLASS (klass);
+
+  file_class->open = gtk_no_media_file_open;
+}
+
+static void
+gtk_no_media_file_init (GtkNoMediaFile *video)
+{
+}
+
diff --git a/gtk/gtknomediafileprivate.h b/gtk/gtknomediafileprivate.h
new file mode 100644 (file)
index 0000000..0bb6f73
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright © 2018 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte@gnome.org>
+ */
+
+#ifndef __GTK_NO_MEDIA_FILE_H__
+#define __GTK_NO_MEDIA_FILE_H__
+
+#include <gtk/gtkmediafile.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_NO_MEDIA_FILE (gtk_no_media_file_get_type ())
+
+G_DECLARE_FINAL_TYPE (GtkNoMediaFile, gtk_no_media_file, GTK, NO_MEDIA_FILE, GtkMediaFile)
+
+G_END_DECLS
+
+#endif /* __GTK_NO_MEDIA_FILE_H__ */
index 6ea234c1b51bdbd6444637da16460696c924867b..f62afe4c3d8d6a30a80b05cc2e3d59fbf44a53d6 100644 (file)
@@ -265,6 +265,7 @@ gtk_public_sources = files([
   'gtkmodules.c',
   'gtkmountoperation.c',
   'gtknativedialog.c',
+  'gtknomediafile.c',
   'gtknotebook.c',
   'gtkorientable.c',
   'gtkoverlay.c',
index 68fce04f46cba5d5610332c106b1aff06a7aea3b..c2d0e51338c42a8349782165138ad877ff8315d0 100644 (file)
@@ -589,6 +589,7 @@ if cloudproviders_enabled
   endif
 endif
 
+
 subdir('gdk')
 subdir('gsk')
 subdir('gtk')
@@ -705,6 +706,7 @@ summary = [
   '  Enabled backends: @0@'.format(pkg_targets.strip()),
   '    Vulkan support: @0@'.format(have_vulkan),
   '    Print backends: @0@'.format(' '.join(print_backends)),
+  '    Media backends: @0@'.format(' '.join(media_backends)),
   '             Tests: @0@'.format(get_option('build-tests')),
   '     Documentation: @0@'.format(get_option('documentation')),
   '             Demos: @0@'.format(get_option('demos')),
index 74e5f21b3273d284ede37e2a9357debb2b57c58b..4a6e322c16fd5cd7fa1faa38675be64a8c8ce175 100644 (file)
@@ -10,6 +10,10 @@ option('win32-backend', type: 'boolean', value: true,
 option('quartz-backend', type: 'boolean', value: true,
   description : 'Enable the macOS gdk backend (only when building on macOS)')
 
+# Media backends
+option('media', type: 'string', value: 'none',
+  description : 'Build the specified media engines (comma-separated list, "all", or "none")')
+
 # Optional dependencies
 option('vulkan', type: 'combo', choices : ['yes', 'no', 'auto'], value : 'auto',
   description : 'Enable support for the Vulkan graphics API')
diff --git a/modules/media/meson.build b/modules/media/meson.build
new file mode 100644 (file)
index 0000000..efab5a3
--- /dev/null
@@ -0,0 +1,23 @@
+all_media_backends = [
+]
+
+enabled_media_backends = get_option('media').split(',')
+
+if enabled_media_backends.contains('none')
+  media_backends = []
+elif enabled_media_backends.contains('all')
+  media_backends = all_media_backends
+else
+  media_backends = []
+  foreach b: enabled_media_backends
+    if all_media_backends.contains(b)
+      media_backends += b
+    else
+      error('No media backend named "@0@" exists.'.format (b))
+    endif
+  endforeach
+endif
+
+media_subdir = 'gtk-4.0/@0@/media'.format(gtk_binary_version)
+media_install_dir = join_paths(get_option('libdir'), media_subdir)
+
index cde966a094d05d84b91d9884a28b0bd1c2d6bf87..670f9fd96c567d1553d39e666260963b47de2022 100644 (file)
@@ -1,3 +1,5 @@
 if os_unix
   subdir('printbackends')
 endif
+
+subdir('media')