[gimp] Move initialization order



commit 825ea9dc1c9da971eb8e75205c130674ee37e396
Author: Luca Bacci <luca bacci982 gmail com>
Date:   Sat Aug 14 12:29:17 2021 +0200

    Move initialization order

 app/app.c  |  60 -----------------------------------
 app/main.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 104 insertions(+), 61 deletions(-)
---
diff --git a/app/app.c b/app/app.c
index a1cc35e689..16a3a1b996 100644
--- a/app/app.c
+++ b/app/app.c
@@ -34,10 +34,6 @@
 #include <gio/gio.h>
 #include <gegl.h>
 
-#ifndef GIMP_CONSOLE_COMPILATION
-#include <gtk/gtk.h>
-#endif
-
 #include <gdk-pixbuf/gdk-pixbuf.h>
 
 #ifdef G_OS_WIN32
@@ -52,7 +48,6 @@
 
 #include "core/core-types.h"
 
-#include "config/gimpearlyrc.h"
 #include "config/gimprc.h"
 
 #include "gegl/gimp-gegl.h"
@@ -72,7 +67,6 @@
 
 #include "app.h"
 #include "errors.h"
-#include "language.h"
 #include "sanity.h"
 #include "gimp-debug.h"
 
@@ -198,8 +192,6 @@ app_run (const gchar         *full_prog_name,
   GFile              *default_folder = NULL;
   GFile              *gimpdir;
   const gchar        *abort_message;
-  GimpEarlyRc        *earlyrc;
-  gchar              *language   = NULL;
   GError             *font_error = NULL;
 
   if (filenames && filenames[0] && ! filenames[1] &&
@@ -222,58 +214,6 @@ app_run (const gchar         *full_prog_name,
       filenames = NULL;
     }
 
-  /* Here we do a first pass on "gimprc" file for the sole purpose
-   * of getting some configuration data that's required during early
-   * initialization, before the gimp singleton is constructed
-   */
-  earlyrc = gimp_early_rc_new (alternate_system_gimprc,
-                               alternate_gimprc,
-                               be_verbose);
-
-  /* Language needs to be determined first, before any GimpContext is
-   * instantiated (which happens when the Gimp object is created)
-   * because its properties need to be properly localized in the
-   * settings language (if different from system language). Otherwise we
-   * end up with pieces of GUI always using the system language (cf. bug
-   * 787457)
-   */
-  language = gimp_early_rc_get_language (earlyrc);
-
-  /*  change the locale if a language if specified  */
-  language_init (language);
-  if (language)
-    g_free (language);
-
-#if defined (G_OS_WIN32) && !defined (GIMP_CONSOLE_COMPILATION)
-
-#if GTK_MAJOR_VERSION > 3
-#warning For GTK4 and above use the proper backend-specific API instead of the GDK_WIN32_TABLET_INPUT_API 
environment variable
-#endif
-
-  /* Support for Windows Ink was introduced in GTK3 3.24.30
-   */
-  if (gtk_get_major_version () == 3 &&
-      (gtk_get_minor_version () > 24 ||
-      (gtk_get_minor_version () == 24 &&
-       gtk_get_micro_version () >= 30)))
-    {
-      GimpWin32PointerInputAPI api = gimp_early_rc_get_win32_pointer_input_api (earlyrc);;
-
-      switch (api)
-        {
-        case GIMP_WIN32_POINTER_INPUT_API_WINTAB:
-          g_setenv ("GDK_WIN32_TABLET_INPUT_API", "wintab", TRUE);
-          break;
-        case GIMP_WIN32_POINTER_INPUT_API_WINDOWS_INK:
-          g_setenv ("GDK_WIN32_TABLET_INPUT_API", "winpointer", TRUE);
-          break;
-        }
-    }
-
-#endif
-
-  g_object_unref (earlyrc);
-
   /*  Create an instance of the "Gimp" object which is the root of the
    *  core object system
    */
diff --git a/app/main.c b/app/main.c
index 9338b3e2e6..5de0b3027e 100644
--- a/app/main.c
+++ b/app/main.c
@@ -44,7 +44,7 @@
 #endif /* __APPLE__ */
 
 #ifndef GIMP_CONSOLE_COMPILATION
-#include <gdk/gdk.h>
+#include <gtk/gtk.h>
 #else
 #include <gdk-pixbuf/gdk-pixbuf.h>
 #endif
@@ -55,6 +55,7 @@
 
 #include "pdb/pdb-types.h"
 
+#include "config/gimpearlyrc.h"
 #include "config/gimpconfig-dump.h"
 
 #include "core/gimp.h"
@@ -66,6 +67,7 @@
 
 #include "about.h"
 #include "app.h"
+#include "language.h"
 #include "sanity.h"
 #include "signals.h"
 #include "unique.h"
@@ -404,6 +406,96 @@ gimp_macos_setenv (const char * progname)
 }
 #endif
 
+/* gimp_early_configuration () is executed as soon as we can read
+ * the "gimprc" files, but before any library initialization takes
+ * place
+ */
+static void
+gimp_early_configuration (void)
+{
+  GFile       *system_gimprc_file = NULL;
+  GFile       *user_gimprc_file   = NULL;
+  GimpEarlyRc *earlyrc;
+  gchar       *language;
+
+  if (system_gimprc)
+    system_gimprc_file = g_file_new_for_commandline_arg (system_gimprc);
+
+  if (user_gimprc)
+    user_gimprc_file = g_file_new_for_commandline_arg (user_gimprc);
+
+  /* GimpEarlyRc is reponsible for reading "gimprc" files for the
+   * sole purpose of getting some configuration data that is needed
+   * in the early initialization phase
+   */
+  earlyrc = gimp_early_rc_new (system_gimprc_file,
+                               user_gimprc_file,
+                               be_verbose);
+
+  /* Language needs to be determined first, before any GimpContext is
+   * instantiated (which happens when the Gimp object is created)
+   * because its properties need to be properly localized in the
+   * settings language (if different from system language). Otherwise we
+   * end up with pieces of GUI always using the system language (cf. bug
+   * 787457)
+   */
+  language = gimp_early_rc_get_language (earlyrc);
+
+  /*  change the locale if a language if specified  */
+  language_init (language);
+  if (language)
+    g_free (language);
+
+#if defined (G_OS_WIN32) && !defined (GIMP_CONSOLE_COMPILATION)
+
+#if GTK_MAJOR_VERSION > 3
+#warning For GTK4 and above use the proper backend-specific API instead of the GDK_WIN32_TABLET_INPUT_API 
environment variable
+#endif
+
+  /* Set a GdkWin32-specific environment variable to specify
+   * the desired pen / touch input API to use on Windows
+  */
+  if (gtk_get_major_version () == 3 &&
+      (gtk_get_minor_version () > 24 ||
+       (gtk_get_minor_version () == 24 &&
+        gtk_get_micro_version () >= 30)))
+    {
+      GimpWin32PointerInputAPI api = gimp_early_rc_get_win32_pointer_input_api (earlyrc);
+
+      switch (api)
+        {
+        case GIMP_WIN32_POINTER_INPUT_API_WINTAB:
+          g_setenv ("GDK_WIN32_TABLET_INPUT_API", "wintab", TRUE);
+          break;
+        case GIMP_WIN32_POINTER_INPUT_API_WINDOWS_INK:
+          g_setenv ("GDK_WIN32_TABLET_INPUT_API", "winpointer", TRUE);
+          break;
+        }
+    }
+
+#endif
+
+  g_object_unref (earlyrc);
+
+  if (system_gimprc_file)
+    g_object_unref (system_gimprc_file);
+
+  if (user_gimprc_file)
+    g_object_unref (user_gimprc_file);
+}
+
+static gboolean
+gimp_options_group_parse_hook (GOptionContext   *context,
+                               GOptionGroup     *group,
+                               gpointer          data,
+                               GError          **error)
+{
+  /*  early initialization from data stored in "gimprc" files  */
+  gimp_early_configuration ();
+
+  return TRUE;
+}
+
 int
 main (int    argc,
       char **argv)
@@ -414,6 +506,7 @@ main (int    argc,
   gchar          *basename;
   GFile          *system_gimprc_file = NULL;
   GFile          *user_gimprc_file   = NULL;
+  GOptionGroup   *gimp_group         = NULL;
   gchar          *backtrace_file     = NULL;
   gint            i;
 
@@ -600,6 +693,16 @@ main (int    argc,
 
   g_option_context_add_main_entries (context, main_entries, GETTEXT_PACKAGE);
 
+  /* The GIMP option group is just an empty option group, created for the sole
+   * purpose of running a post-parse hook before any other of dependant libraries
+   * are run. This makes it possible to apply options from configuration data
+   * obtained from "gimprc" files, before other libraries have a chance to run
+   * some of their intialization code.
+   */
+  gimp_group = g_option_group_new ("gimp", "", "", NULL, NULL);
+  g_option_group_set_parse_hooks (gimp_group, NULL, gimp_options_group_parse_hook);
+  g_option_context_add_group (context, gimp_group);
+
   app_libs_init (context, no_interface);
 
   if (! g_option_context_parse_strv (context, &argv, &error))


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