[gimp] libgimp: API to create GimpParam from values.



commit 3a9ae8cc6988b7033ca03d8a436cdafe94be8501
Author: Jehan <jehan girinstud io>
Date:   Sun Jul 28 15:10:23 2019 +0200

    libgimp: API to create GimpParam from values.
    
    The `data` property of a GimpParam is a union. Unfortunately setting a
    union is not supported by GObject Introspection yet. So I create some
    APIs to create GimpParam-s from values. Note that this is temporary API
    (i.e. it may be removed before GIMP 3 release) since we likely won't use
    this GimpParam type anymore with the new plug-in API. But for now, this
    is necessary, at least for testing and porting Python plug-ins.
    
    Also for GimpParam to be actually introspectable, I had to make it a
    boxed type, but since no length information is available for various
    variants of the type (arrays, whose length information is a separate
    parameter), the copy and free functions are basically broken or leaking
    respectively for all types requiring a length.
    
    Bottom line: this is ugly and we really need a new introspectable
    parameter type. But for now, it allows to start porting some of our
    Python plug-ins.

 libgimp/gimp.c | 206 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 libgimp/gimp.h |  29 ++++++--
 2 files changed, 230 insertions(+), 5 deletions(-)
---
diff --git a/libgimp/gimp.c b/libgimp/gimp.c
index 63f6331dbf..36c8d419cf 100644
--- a/libgimp/gimp.c
+++ b/libgimp/gimp.c
@@ -204,6 +204,8 @@ static gboolean   gimp_extension_read          (GIOChannel      *channel,
 static void       gimp_set_pdb_error           (const GimpParam *return_vals,
                                                 gint             n_return_vals);
 
+static gpointer   gimp_param_copy              (gpointer         boxed);
+static void       gimp_param_free              (gpointer         boxed);
 
 #if defined G_OS_WIN32 && defined HAVE_EXCHNDL
 static LPTOP_LEVEL_EXCEPTION_FILTER  _prevExceptionFilter    = NULL;
@@ -291,6 +293,126 @@ gimp_plug_in_info_set_callbacks (GimpPlugInInfo *info,
   info->run_proc   = run_proc;
 }
 
+/**
+ * gimp_param_from_int32:
+ * @value: the #gint32 value to set.
+ *
+ * The procedure creates a new #GimpParam for a int32 value.
+ *
+ * Returns: (transfer full): A newly allocated #GimpParam.
+ *
+ * Since: 3.0
+ **/
+GimpParam *
+gimp_param_from_int32 (gint32 value)
+{
+  GimpParam * param = g_new0 (GimpParam, 1);
+
+  param->type = GIMP_PDB_INT32;
+  param->data.d_int32 = value;
+
+  return param;
+}
+
+/**
+ * gimp_param_get_int32:
+ * @param: the #GimpParam.
+ *
+ * Unwrap the integer value contained in @param. Running this function
+ * if @param is not an int32 #GimpParam is a programming error.
+ *
+ * Returns: the #gint32 value contained in @param.
+ *
+ * Since: 3.0
+ **/
+gint32
+gimp_param_get_int32 (GimpParam *param)
+{
+  g_return_val_if_fail (param->type == GIMP_PDB_INT32, 0);
+
+  return param->data.d_int32;
+}
+
+/**
+ * gimp_param_from_status:
+ * @value: the #GimpPDBStatusType value to set.
+ *
+ * The procedure creates a new #GimpParam for a status value.
+ *
+ * Returns: (transfer full): A newly allocated #GimpParam.
+ *
+ * Since: 3.0
+ **/
+GimpParam *
+gimp_param_from_status (GimpPDBStatusType value)
+{
+  GimpParam * param = g_new0 (GimpParam, 1);
+
+  param->type = GIMP_PDB_STATUS;
+  param->data.d_status = value;
+
+  return param;
+}
+
+/**
+ * gimp_param_get_status:
+ * @param: the #GimpParam.
+ *
+ * Unwrap the status value contained in @param. Running this function
+ * if @param is not a status #GimpParam is a programming error.
+ *
+ * Returns: the #GimpPDBStatusType value contained in @param.
+ *
+ * Since: 3.0
+ **/
+GimpPDBStatusType
+gimp_param_get_status (GimpParam *param)
+{
+  g_return_val_if_fail (param->type == GIMP_PDB_STATUS, 0);
+
+  return param->data.d_status;
+}
+
+/**
+ * gimp_param_from_string:
+ * @value: the string value to set.
+ *
+ * The procedure creates a new #GimpParam for a string value.
+ *
+ * Returns: (transfer full): A newly allocated #GimpParam.
+ *
+ * Since: 3.0
+ **/
+GimpParam *
+gimp_param_from_string (gchar *value)
+{
+  GimpParam * param = g_new0 (GimpParam, 1);
+
+  param->type = GIMP_PDB_STRING;
+  param->data.d_string = g_strdup (value);
+
+  return param;
+}
+
+/**
+ * gimp_param_get_string:
+ * @param: the #GimpParam.
+ *
+ * Unwrap the string value contained in @param. Running this function
+ * if @param is not a string #GimpParam is a programming error.
+ *
+ * Returns: the string value contained in @param.
+ *
+ * Since: 3.0
+ **/
+gchar *
+gimp_param_get_string (GimpParam *param)
+{
+  g_return_val_if_fail (param->type == GIMP_PDB_STRING, NULL);
+
+  return param->data.d_string;
+}
+
 /**
  * gimp_main:
  * @plug_in_type: the type of the #GimpPlugIn subclass of the plug-in
@@ -2582,3 +2704,87 @@ gimp_set_pdb_error (const GimpParam *return_vals,
       break;
     }
 }
+
+/* Define boxed type functions. */
+
+static gpointer
+gimp_param_copy (gpointer boxed)
+{
+  GimpParam *param = boxed;
+  GimpParam *new_param;
+
+  new_param = g_slice_new (GimpParam);
+  new_param->type = param->type;
+  switch (param->type)
+    {
+    case GIMP_PDB_STRING:
+      new_param->data.d_string = g_strdup (param->data.d_string);
+      break;
+    case GIMP_PDB_INT32ARRAY:
+    case GIMP_PDB_INT16ARRAY:
+    case GIMP_PDB_INT8ARRAY:
+    case GIMP_PDB_FLOATARRAY:
+    case GIMP_PDB_COLORARRAY:
+    case GIMP_PDB_STRINGARRAY:
+      /* XXX: we can't copy these because we don't know the size, and
+       * we are bounded by the GBoxed copy function signature.
+       * Anyway this is only temporary until we replace GimpParam in the
+       * new API.
+       */
+      g_return_val_if_reached (new_param);
+      break;
+    default:
+      new_param->data = param->data;
+      break;
+    }
+
+  return new_param;
+}
+
+static void
+gimp_param_free (gpointer boxed)
+{
+  GimpParam *param = boxed;
+
+  switch (param->type)
+    {
+    case GIMP_PDB_STRING:
+      g_free (param->data.d_string);
+      break;
+    case GIMP_PDB_INT32ARRAY:
+      g_free (param->data.d_int32array);
+      break;
+    case GIMP_PDB_INT16ARRAY:
+      g_free (param->data.d_int16array);
+      break;
+    case GIMP_PDB_INT8ARRAY:
+      g_free (param->data.d_int8array);
+      break;
+    case GIMP_PDB_FLOATARRAY:
+      g_free (param->data.d_floatarray);
+      break;
+    case GIMP_PDB_COLORARRAY:
+      g_free (param->data.d_colorarray);
+      break;
+    case GIMP_PDB_STRINGARRAY:
+      /* XXX: we also want to free each element string. Unfortunately
+       * this type is not zero-terminated or anything of the sort to
+       * determine the number of elements.
+       * It uses the previous parameter, but we cannot have such value
+       * in a GBoxed's free function.
+       * Since this is all most likely temporary code until we update
+       * the plug-in API, let's just leak for now.
+       */
+      g_free (param->data.d_stringarray);
+      break;
+    default:
+      /* Pass-through. */
+      break;
+    }
+
+  g_slice_free (GimpParam, boxed);
+}
+
+G_DEFINE_BOXED_TYPE (GimpParam, gimp_param,
+                     gimp_param_copy,
+                     gimp_param_free)
diff --git a/libgimp/gimp.h b/libgimp/gimp.h
index e93cb06a5f..ff5c847b86 100644
--- a/libgimp/gimp.h
+++ b/libgimp/gimp.h
@@ -171,7 +171,17 @@ struct _GimpParam
   GimpParamData  data;
 };
 
+/**
+ * GIMP_TYPE_PARAM:
+ *
+ * Boxed type representing parameters and returned values, as passed
+ * through the plug-in legacy API.
+ *
+ * Since: 3.0
+ */
+#define GIMP_TYPE_PARAM (gimp_param_get_type ())
 
+GType gimp_param_get_type (void) G_GNUC_CONST;
 
 /**
  * GIMP_MAIN:
@@ -299,11 +309,20 @@ struct _GimpParam
 #endif
 
 
-void           gimp_plug_in_info_set_callbacks    (GimpPlugInInfo *info,
-                                                   GimpInitProc    init_proc,
-                                                   GimpQuitProc    quit_proc,
-                                                   GimpQueryProc   query_proc,
-                                                   GimpRunProc     run_proc);
+void                gimp_plug_in_info_set_callbacks    (GimpPlugInInfo    *info,
+                                                        GimpInitProc       init_proc,
+                                                        GimpQuitProc       quit_proc,
+                                                        GimpQueryProc      query_proc,
+                                                        GimpRunProc        run_proc);
+
+GimpParam         * gimp_param_from_int32              (gint32             value);
+gint32              gimp_param_get_int32               (GimpParam         *param);
+
+GimpParam         * gimp_param_from_status             (GimpPDBStatusType  value);
+GimpPDBStatusType   gimp_param_get_status              (GimpParam         *param);
+
+GimpParam         * gimp_param_from_string             (gchar             *value);
+gchar             * gimp_param_get_string              (GimpParam         *param);
 
 /* The main procedure that must be called with the PLUG_IN_INFO
  * structure and the 'argc' and 'argv' that are passed to "main".


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