[gimp] libgimp: add initial version of a GimpPlugIn class
- From: Michael Natterer <mitch src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gimp] libgimp: add initial version of a GimpPlugIn class
- Date: Thu, 25 Jul 2019 21:11:17 +0000 (UTC)
commit f2d399b17b5dfc9c0a87673bd3d5fa5c426f0dcf
Author: Michael Natterer <mitch gimp org>
Date: Thu Jul 25 23:07:24 2019 +0200
libgimp: add initial version of a GimpPlugIn class
The new way of doing plug-ins:
- subclass GimpPlugIn in your plug-in
- implement its query() and run() methods, run() will move to a
new GimpProcedure class soon
- instead of MAIN(), say GIMP_MAIN(YOUR_PLUG_IN_TYPE)
Instead of keeping around a GimpPlugInInfo struct, libgimp will
create an instance of your plug-in class, keep it around during
the plug-in's lifetime, and call its virtual functions.
libgimp/Makefile.am | 5 ++
libgimp/gimp.c | 125 ++++++++++++++++++++++++++++++++++++-------
libgimp/gimp.def | 1 +
libgimp/gimp.h | 91 ++++++++++++++++++++++++++++---
libgimp/gimpplugin-private.c | 71 ++++++++++++++++++++++++
libgimp/gimpplugin-private.h | 41 ++++++++++++++
libgimp/gimpplugin.c | 49 +++++++++++++++++
libgimp/gimpplugin.h | 51 ++++++++++++++++++
libgimp/gimptypes.h | 3 ++
9 files changed, 412 insertions(+), 25 deletions(-)
---
diff --git a/libgimp/Makefile.am b/libgimp/Makefile.am
index df17810098..462e14c851 100644
--- a/libgimp/Makefile.am
+++ b/libgimp/Makefile.am
@@ -106,10 +106,15 @@ libgimp_sources = \
libgimp_built_sources = \
gimpenums.c
+libgimp_private_sources = \
+ gimpplugin-private.c \
+ gimpplugin-private.h
+
libgimp_extra_sources = gimpenums.c.tail
libgimp_@GIMP_API_VERSION@_la_SOURCES = \
$(libgimp_built_sources) \
+ $(libgimp_private_sources) \
$(libgimp_sources)
libgimpui_sources = \
diff --git a/libgimp/gimp.c b/libgimp/gimp.c
index a9cc9e6136..bd3a0a1f52 100644
--- a/libgimp/gimp.c
+++ b/libgimp/gimp.c
@@ -118,6 +118,7 @@
#include "libgimpbase/gimpwire.h"
#include "gimp.h"
+#include "gimpplugin-private.h"
#include "gimpunitcache.h"
#include "libgimp-intl.h"
@@ -157,6 +158,11 @@ void gimp_read_expect_msg (GimpWireMessage *msg,
gint type);
+static gint gimp_main_internal (GType plug_in_type,
+ const GimpPlugInInfo *info,
+ gint argc,
+ gchar *argv[]);
+
static void gimp_close (void);
static void gimp_debug_stop (void);
static void gimp_message_func (const gchar *log_domain,
@@ -249,7 +255,8 @@ static const GDebugKey gimp_debug_keys[] =
{ "on", GIMP_DEBUG_DEFAULT }
};
-static GimpPlugInInfo PLUG_IN_INFO;
+static GimpPlugIn *PLUG_IN;
+static GimpPlugInInfo PLUG_IN_INFO;
static GimpPDBStatusType pdb_error_status = GIMP_PDB_SUCCESS;
@@ -284,20 +291,50 @@ gimp_plug_in_info_set_callbacks (GimpPlugInInfo *info,
/**
* gimp_main:
- * @info: the PLUG_IN_INFO structure
+ * @plug_in_type: the type of the #GimpPlugIn subclass of the plug-in
+ * @argc: the number of arguments
+ * @argv: (array length=argc): the arguments
+ *
+ * The main procedure that must be called with the plug-in's
+ * #GimpPlugIn subclass type and the 'argc' and 'argv' that are passed
+ * to "main".
+ *
+ * Returns: an exit status as defined by the C library,
+ * on success EXIT_SUCCESS.
+ **/
+gint
+gimp_main (GType plug_in_type,
+ gint argc,
+ gchar *argv[])
+{
+ return gimp_main_internal (plug_in_type, NULL, argc, argv);
+}
+
+/**
+ * gimp_main_legacy:
+ * @info: the #GimpPlugInInfo structure
* @argc: the number of arguments
* @argv: (array length=argc): the arguments
*
- * The main procedure that must be called with the PLUG_IN_INFO structure
- * and the 'argc' and 'argv' that are passed to "main".
+ * The main procedure that must be called with the #GimpPlugInInfo
+ * structure and the 'argc' and 'argv' that are passed to "main".
*
* Returns: an exit status as defined by the C library,
* on success EXIT_SUCCESS.
**/
gint
-gimp_main (const GimpPlugInInfo *info,
- gint argc,
- gchar *argv[])
+gimp_main_legacy (const GimpPlugInInfo *info,
+ gint argc,
+ gchar *argv[])
+{
+ return gimp_main_internal (G_TYPE_NONE, info, argc, argv);
+}
+
+static gint
+gimp_main_internal (GType plug_in_type,
+ const GimpPlugInInfo *info,
+ gint argc,
+ gchar *argv[])
{
enum
{
@@ -462,9 +499,19 @@ gimp_main (const GimpPlugInInfo *info,
}
#endif
- g_assert (info != NULL);
+ g_assert ((plug_in_type != G_TYPE_NONE && info == NULL) ||
+ (plug_in_type == G_TYPE_NONE && info != NULL));
- PLUG_IN_INFO = *info;
+ if (plug_in_type != G_TYPE_NONE)
+ {
+ PLUG_IN = g_object_new (plug_in_type, NULL);
+
+ g_assert (GIMP_IS_PLUG_IN (PLUG_IN));
+ }
+ else
+ {
+ PLUG_IN_INFO = *info;
+ }
if ((argc != N_ARGS) || (strcmp (argv[ARG_GIMP], "-gimp") != 0))
{
@@ -676,14 +723,29 @@ gimp_main (const GimpPlugInInfo *info,
if (strcmp (argv[ARG_MODE], "-query") == 0)
{
- if (PLUG_IN_INFO.init_proc)
- gp_has_init_write (_writechannel, NULL);
+ if (PLUG_IN)
+ {
+ if (GIMP_PLUG_IN_GET_CLASS (PLUG_IN)->init)
+ gp_has_init_write (_writechannel, NULL);
+ }
+ else
+ {
+ if (PLUG_IN_INFO.init_proc)
+ gp_has_init_write (_writechannel, NULL);
+ }
if (gimp_debug_flags & GIMP_DEBUG_QUERY)
gimp_debug_stop ();
- if (PLUG_IN_INFO.query_proc)
- (* PLUG_IN_INFO.query_proc) ();
+ if (PLUG_IN)
+ {
+ _gimp_plug_in_query (PLUG_IN);
+ }
+ else
+ {
+ if (PLUG_IN_INFO.query_proc)
+ PLUG_IN_INFO.query_proc ();
+ }
gimp_close ();
@@ -695,8 +757,15 @@ gimp_main (const GimpPlugInInfo *info,
if (gimp_debug_flags & GIMP_DEBUG_INIT)
gimp_debug_stop ();
- if (PLUG_IN_INFO.init_proc)
- (* PLUG_IN_INFO.init_proc) ();
+ if (PLUG_IN)
+ {
+ _gimp_plug_in_init (PLUG_IN);
+ }
+ else
+ {
+ if (PLUG_IN_INFO.init_proc)
+ PLUG_IN_INFO.init_proc ();
+ }
gimp_close ();
@@ -1768,8 +1837,15 @@ gimp_close (void)
if (gimp_debug_flags & GIMP_DEBUG_QUIT)
gimp_debug_stop ();
- if (PLUG_IN_INFO.quit_proc)
- (* PLUG_IN_INFO.quit_proc) ();
+ if (PLUG_IN)
+ {
+ _gimp_plug_in_quit (PLUG_IN);
+ }
+ else
+ {
+ if (PLUG_IN_INFO.quit_proc)
+ PLUG_IN_INFO.quit_proc ();
+ }
#if defined(USE_SYSV_SHM)
@@ -2294,16 +2370,27 @@ gimp_config (GPConfig *config)
static void
gimp_proc_run (GPProcRun *proc_run)
{
- if (PLUG_IN_INFO.run_proc)
+ if (PLUG_IN || PLUG_IN_INFO.run_proc)
{
GPProcReturn proc_return;
GimpParam *return_vals;
gint n_return_vals;
- (* PLUG_IN_INFO.run_proc) (proc_run->name,
+ if (PLUG_IN)
+ {
+ _gimp_plug_in_run (PLUG_IN,
+ proc_run->name,
+ proc_run->nparams,
+ (GimpParam *) proc_run->params,
+ &n_return_vals, &return_vals);
+ }
+ else
+ {
+ PLUG_IN_INFO.run_proc (proc_run->name,
proc_run->nparams,
(GimpParam *) proc_run->params,
&n_return_vals, &return_vals);
+ }
proc_return.name = proc_run->name;
proc_return.nparams = n_return_vals;
diff --git a/libgimp/gimp.def b/libgimp/gimp.def
index 4fc5373829..f94ab8bb28 100644
--- a/libgimp/gimp.def
+++ b/libgimp/gimp.def
@@ -592,6 +592,7 @@ EXPORTS
gimp_plugin_menu_branch_register
gimp_plugin_menu_register
gimp_plugin_set_pdb_error_handler
+ gimp_plug_in_get_type
gimp_plug_in_info_set_callbacks
gimp_procedural_db_dump
gimp_procedural_db_get_data
diff --git a/libgimp/gimp.h b/libgimp/gimp.h
index 251376bac1..9a9143ddcb 100644
--- a/libgimp/gimp.h
+++ b/libgimp/gimp.h
@@ -74,12 +74,14 @@ G_BEGIN_DECLS
* The init procedure is run at every GIMP startup.
*/
typedef void (* GimpInitProc) (void);
+
/**
* GimpQuitProc:
*
* The quit procedure is run each time the plug-in ends.
*/
typedef void (* GimpQuitProc) (void);
+
/**
* GimpQueryProc:
*
@@ -87,6 +89,7 @@ typedef void (* GimpQuitProc) (void);
* time after a plug-in is installed, or if it has been updated.
*/
typedef void (* GimpQueryProc) (void);
+
/**
* GimpRunProc:
* @name: the name of the procedure which has been called.
@@ -176,6 +179,71 @@ struct _GimpParam
+/**
+ * GIMP_MAIN:
+ * @plug_in_type: The #GType of the plug-in's #GimpPlugIn subclass
+ *
+ * A macro that expands to the appropriate main() function for the
+ * platform being compiled for.
+ *
+ * To use this macro, simply place a line that contains just the code
+ *
+ * GIMP_MAIN (MY_TYPE_PLUG_IN)
+ *
+ * at the toplevel of your file. No semicolon should be used.
+ **/
+
+#ifdef G_OS_WIN32
+
+/* Define WinMain() because plug-ins are built as GUI applications. Also
+ * define a main() in case some plug-in still is built as a console
+ * application.
+ */
+# ifdef __GNUC__
+# ifndef _stdcall
+# define _stdcall __attribute__((stdcall))
+# endif
+# endif
+
+# define GIMP_MAIN(plug_in_type) \
+ struct HINSTANCE__; \
+ \
+ int _stdcall \
+ WinMain (struct HINSTANCE__ *hInstance, \
+ struct HINSTANCE__ *hPrevInstance, \
+ char *lpszCmdLine, \
+ int nCmdShow); \
+ \
+ int _stdcall \
+ WinMain (struct HINSTANCE__ *hInstance, \
+ struct HINSTANCE__ *hPrevInstance, \
+ char *lpszCmdLine, \
+ int nCmdShow) \
+ { \
+ return gimp_main (plug_in_type, \
+ _argc, __argv); \
+ } \
+ \
+ int \
+ main (int argc, char *argv[]) \
+ { \
+ /* Use __argc and __argv here, too, as they work \
+ * better with mingw-w64. \
+ */ \
+ return gimp_main (plug_in_type, \
+ __argc, __argv); \
+ }
+#else
+# define GIMP_MAIN(plug_in_type) \
+ int \
+ main (int argc, char *argv[]) \
+ { \
+ return gimp_main (plug_in_type, \
+ argc, argv); \
+ }
+#endif
+
+
/**
* MAIN:
*
@@ -213,7 +281,8 @@ struct _GimpParam
char *lpszCmdLine, \
int nCmdShow) \
{ \
- return gimp_main (&PLUG_IN_INFO, __argc, __argv); \
+ return gimp_main_legacy (&PLUG_IN_INFO, \
+ _argc, __argv); \
} \
\
int \
@@ -222,14 +291,16 @@ struct _GimpParam
/* Use __argc and __argv here, too, as they work \
* better with mingw-w64. \
*/ \
- return gimp_main (&PLUG_IN_INFO, __argc, __argv); \
+ return gimp_main_legacy (&PLUG_IN_INFO, \
+ __argc, __argv); \
}
#else
# define MAIN() \
int \
main (int argc, char *argv[]) \
{ \
- return gimp_main (&PLUG_IN_INFO, argc, argv); \
+ return gimp_main_legacy (&PLUG_IN_INFO, \
+ argc, argv); \
}
#endif
@@ -240,10 +311,18 @@ void gimp_plug_in_info_set_callbacks (GimpPlugInInfo *info,
GimpQueryProc query_proc,
GimpRunProc run_proc);
-/* The main procedure that must be called with the PLUG_IN_INFO structure
- * and the 'argc' and 'argv' that are passed to "main".
+/* The main procedure that must be called with the PLUG_IN_INFO
+ * structure and the 'argc' and 'argv' that are passed to "main".
+ */
+gint gimp_main_legacy (const GimpPlugInInfo *info,
+ gint argc,
+ gchar *argv[]);
+
+/* The main procedure that must be called with the plug-in's
+ * GimpPlugIn subclass type and the 'argc' and 'argv' that are passed
+ * to "main".
*/
-gint gimp_main (const GimpPlugInInfo *info,
+gint gimp_main (GType plug_in_type,
gint argc,
gchar *argv[]);
diff --git a/libgimp/gimpplugin-private.c b/libgimp/gimpplugin-private.c
new file mode 100644
index 0000000000..89ebc6da9c
--- /dev/null
+++ b/libgimp/gimpplugin-private.c
@@ -0,0 +1,71 @@
+/* LIBGIMP - The GIMP Library
+ * Copyright (C) 1995-2003 Peter Mattis and Spencer Kimball
+ *
+ * gimpplugin-private.c
+ *
+ * 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 3 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
+ * <https://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <string.h>
+
+#include "gimp.h"
+#include "gimpplugin-private.h"
+
+
+void
+_gimp_plug_in_init (GimpPlugIn *plug_in)
+{
+ g_return_if_fail (GIMP_IS_PLUG_IN (plug_in));
+
+ if (GIMP_PLUG_IN_GET_CLASS (plug_in)->init)
+ GIMP_PLUG_IN_GET_CLASS (plug_in)->init (plug_in);
+}
+
+void
+_gimp_plug_in_quit (GimpPlugIn *plug_in)
+{
+ g_return_if_fail (GIMP_IS_PLUG_IN (plug_in));
+
+ if (GIMP_PLUG_IN_GET_CLASS (plug_in)->quit)
+ GIMP_PLUG_IN_GET_CLASS (plug_in)->quit (plug_in);
+}
+
+void
+_gimp_plug_in_query (GimpPlugIn *plug_in)
+{
+ g_return_if_fail (GIMP_IS_PLUG_IN (plug_in));
+
+ if (GIMP_PLUG_IN_GET_CLASS (plug_in)->query)
+ GIMP_PLUG_IN_GET_CLASS (plug_in)->query (plug_in);
+}
+
+void
+_gimp_plug_in_run (GimpPlugIn *plug_in,
+ const gchar *name,
+ gint n_params,
+ const GimpParam *param,
+ gint *n_return_vals,
+ GimpParam **return_vals)
+{
+ g_return_if_fail (GIMP_IS_PLUG_IN (plug_in));
+
+ if (GIMP_PLUG_IN_GET_CLASS (plug_in)->run)
+ GIMP_PLUG_IN_GET_CLASS (plug_in)->run (plug_in,
+ name,
+ n_params, param,
+ n_return_vals, return_vals);
+}
diff --git a/libgimp/gimpplugin-private.h b/libgimp/gimpplugin-private.h
new file mode 100644
index 0000000000..9f486d28ec
--- /dev/null
+++ b/libgimp/gimpplugin-private.h
@@ -0,0 +1,41 @@
+/* LIBGIMP - The GIMP Library
+ * Copyright (C) 1995-2003 Peter Mattis and Spencer Kimball
+ *
+ * gimpplugin-private.h
+ *
+ * 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 3 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
+ * <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GIMP_PLUG_IN_PRIVATE_H__
+#define __GIMP_PLUG_IN_PRIVATE_H__
+
+G_BEGIN_DECLS
+
+void _gimp_plug_in_init (GimpPlugIn *plug_in);
+void _gimp_plug_in_quit (GimpPlugIn *plug_in);
+void _gimp_plug_in_query (GimpPlugIn *plug_in);
+
+/* temp */
+void _gimp_plug_in_run (GimpPlugIn *plug_in,
+ const gchar *name,
+ gint n_params,
+ const GimpParam *param,
+ gint *n_return_vals,
+ GimpParam **return_vals);
+
+
+G_END_DECLS
+
+#endif /* __GIMP_PLUG_IN_PRIVATE_H__ */
diff --git a/libgimp/gimpplugin.c b/libgimp/gimpplugin.c
index 6b938c4562..0c78424c4c 100644
--- a/libgimp/gimpplugin.c
+++ b/libgimp/gimpplugin.c
@@ -24,6 +24,55 @@
#include "gimp.h"
+
+/**
+ * SECTION: gimpplugin
+ * @title: GimpPlugIn
+ * @short_description: The base class for plug-ins to derive from
+ *
+ * The base class for plug-ins to derive from. Manages the plug-in's
+ * #GimpProcedure objects.
+ *
+ * Since: 3.0
+ **/
+
+
+struct _GimpPlugInPrivate
+{
+ gint foo;
+};
+
+
+static void gimp_plug_in_finalize (GObject *object);
+
+
+G_DEFINE_TYPE_WITH_PRIVATE (GimpPlugIn, gimp_plug_in, G_TYPE_OBJECT)
+
+#define parent_class gimp_plug_in_parent_class
+
+
+static void
+gimp_plug_in_class_init (GimpPlugInClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = gimp_plug_in_finalize;
+}
+
+static void
+gimp_plug_in_init (GimpPlugIn *plug_in)
+{
+ plug_in->priv = gimp_plug_in_get_instance_private (plug_in);
+}
+
+static void
+gimp_plug_in_finalize (GObject *object)
+{
+}
+
+
+/* unrelated old API */
+
gboolean
gimp_plugin_icon_register (const gchar *procedure_name,
GimpIconType icon_type,
diff --git a/libgimp/gimpplugin.h b/libgimp/gimpplugin.h
index 32b9936471..a6e0f10d99 100644
--- a/libgimp/gimpplugin.h
+++ b/libgimp/gimpplugin.h
@@ -30,6 +30,57 @@ G_BEGIN_DECLS
/* For information look into the C source or the html documentation */
+#define GIMP_TYPE_PLUG_IN (gimp_plug_in_get_type ())
+#define GIMP_PLUG_IN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_PLUG_IN, GimpPlugIn))
+#define GIMP_PLUG_IN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_PLUG_IN, GimpPlugInClass))
+#define GIMP_IS_PLUG_IN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_PLUG_IN))
+#define GIMP_IS_PLUG_IN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_PLUG_IN))
+#define GIMP_PLUG_IN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_PLUG_IN, GimpPlugInClass))
+
+
+typedef struct _GimpPlugInClass GimpPlugInClass;
+typedef struct _GimpPlugInPrivate GimpPlugInPrivate;
+
+struct _GimpPlugIn
+{
+ GObject parent_instance;
+
+ GimpPlugInPrivate *priv;
+};
+
+struct _GimpPlugInClass
+{
+ GObjectClass parent_class;
+
+ void (* init) (GimpPlugIn *plug_in);
+ void (* quit) (GimpPlugIn *plug_in);
+ void (* query) (GimpPlugIn *plug_in);
+
+ /* temp */
+ void (* run) (GimpPlugIn *plug_in,
+ const gchar *name,
+ gint n_params,
+ const GimpParam *param,
+ gint *n_return_vals,
+ GimpParam **return_vals);
+
+ /* Padding for future expansion */
+ void (* _gimp_reserved1) (void);
+ void (* _gimp_reserved2) (void);
+ void (* _gimp_reserved3) (void);
+ void (* _gimp_reserved4) (void);
+ void (* _gimp_reserved5) (void);
+ void (* _gimp_reserved6) (void);
+ void (* _gimp_reserved7) (void);
+ void (* _gimp_reserved8) (void);
+};
+
+
+GType gimp_plug_in_get_type (void) G_GNUC_CONST;
+
+
+/* unrelated old API */
+
gboolean gimp_plugin_icon_register (const gchar *procedure_name,
GimpIconType icon_type,
const guint8 *icon_data);
diff --git a/libgimp/gimptypes.h b/libgimp/gimptypes.h
index bf5993957a..73fc4bcc14 100644
--- a/libgimp/gimptypes.h
+++ b/libgimp/gimptypes.h
@@ -28,6 +28,9 @@ G_BEGIN_DECLS
/* For information look into the html documentation */
+typedef struct _GimpPlugIn GimpPlugIn;
+typedef struct _GimpProcedure GimpProcedure;
+
typedef struct _GimpPlugInInfo GimpPlugInInfo;
typedef struct _GimpParamDef GimpParamDef;
typedef struct _GimpParamRegion GimpParamRegion;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]