1
1
mirror of https://gitlab.gnome.org/GNOME/gimp.git synced 2025-10-06 05:22:40 +02:00
Files
gimp/app/core/gimp-batch.c

308 lines
9.1 KiB
C
Raw Permalink Normal View History

/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <stdlib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>
#include "libgimpbase/gimpbase.h"
#include "core-types.h"
#include "gimp.h"
#include "gimp-batch.h"
#include "gimpparamspecs.h"
app/pdb/Makefile.am app/pdb/pdb-types.h new object GimpPDB which keeps all 2006-04-26 Michael Natterer <mitch@gimp.org> * app/pdb/Makefile.am * app/pdb/pdb-types.h * app/pdb/gimppdb.[ch]: new object GimpPDB which keeps all procedures and functions to register and run them. Renamed all functions and did some cleanups. * app/pdb/gimp-pdb.[ch] * app/core/gimp.[ch]: removed the same stuff here. * app/pdb/gimp-pdb-query.[ch]: removed these files... * app/pdb/gimppdb-query.[ch]: ...added here as members of GimpPDB. * app/pdb/gimp-pdb-compat.h: fix include guard. * app/batch.c * app/actions/vectors-commands.c * app/dialogs/about-dialog.c * app/file/file-open.c * app/file/file-save.c * app/plug-in/plug-in-message.c * app/plug-in/plug-ins.c * app/widgets/gimpfiledialog.c * app/widgets/gimphelp.c * app/xcf/xcf.c * tools/pdbgen/pdb/brush_select.pdb * tools/pdbgen/pdb/fileops.pdb * tools/pdbgen/pdb/font_select.pdb * tools/pdbgen/pdb/gradient_select.pdb * tools/pdbgen/pdb/palette_select.pdb * tools/pdbgen/pdb/pattern_select.pdb * tools/pdbgen/pdb/procedural_db.pdb: changed includes and function calls accordingly. * tools/pdbgen/app.pl: pass around GimpPDB instead of Gimp pointers to register the internal procedures with. Changed some newlines in the generated code. * app/pdb/*_cmds.c * app/pdb/internal_procs.[ch]: regenerated. * app/core/gimppdbprogress.[ch] * app/widgets/gimppdbdialog.[ch]: added "pdb" CONSTRUCT_ONLY properties. * app/plug-in/plug-in-progress.c * app/gui/gui-vtable.c: pass gimp->pdb when creating them. * app/widgets/gimpbrushselect.c * app/widgets/gimpfontselect.c * app/widgets/gimpgradientselect.c * app/widgets/gimppaletteselect.c * app/widgets/gimppatternselect.c: use the new local pdb pointers instead of some foo->bar->gimp->pdb overkill.
2006-04-26 09:13:47 +00:00
#include "pdb/gimppdb.h"
#include "pdb/gimpprocedure.h"
#include "plug-in/gimppluginmanager.h"
#include "plug-in/gimppluginprocedure.h"
#include "gimp-intl.h"
static void gimp_batch_exit_after_callback (Gimp *gimp) G_GNUC_NORETURN;
static gint gimp_batch_run_cmd (Gimp *gimp,
const gchar *proc_name,
GimpProcedure *procedure,
GimpRunMode run_mode,
const gchar *cmd);
gint
gimp_batch_run (Gimp *gimp,
const gchar *batch_interpreter,
const gchar **batch_commands)
{
GimpProcedure *eval_proc;
GSList *batch_procedures;
GSList *iter;
gulong exit_id;
gint retval = EXIT_SUCCESS;
if (! batch_commands || ! batch_commands[0])
return retval;
batch_procedures = gimp_plug_in_manager_get_batch_procedures (gimp->plug_in_manager);
if (g_slist_length (batch_procedures) == 0)
{
g_message (_("No batch interpreters are available. "
"Batch mode disabled."));
retval = 69; /* EX_UNAVAILABLE - service unavailable (sysexits.h) */
return retval;
}
if (! batch_interpreter)
{
batch_interpreter = g_getenv ("GIMP_BATCH_INTERPRETER");
if (! batch_interpreter)
{
if (g_slist_length (batch_procedures) == 1)
{
batch_interpreter = gimp_object_get_name (batch_procedures->data);;
if (gimp->be_verbose)
g_printerr (_("No batch interpreter specified, using "
"'%s'.\n"), batch_interpreter);
}
else
{
retval = 64; /* EX_USAGE - command line usage error */
g_print ("%s\n\n%s\n",
_("No batch interpreter specified."),
_("Available interpreters are:"));
for (iter = batch_procedures; iter; iter = iter->next)
{
GimpPlugInProcedure *proc = iter->data;
gchar *locale_name;
locale_name = g_locale_from_utf8 (proc->batch_interpreter_name,
-1, NULL, NULL, NULL);
g_print ("- %s (%s)\n",
gimp_object_get_name (iter->data),
locale_name ? locale_name : proc->batch_interpreter_name);
g_free (locale_name);
}
g_print ("\n%s\n",
_("Specify one of these interpreters as --batch-interpreter option."));
return retval;
}
}
}
for (iter = batch_procedures; iter; iter = iter->next)
{
if (g_strcmp0 (gimp_object_get_name (iter->data),
batch_interpreter) == 0)
break;
}
if (iter == NULL)
{
retval = 69; /* EX_UNAVAILABLE - service unavailable (sysexits.h) */
g_print (_("The procedure '%s' is not a valid batch interpreter."),
batch_interpreter);
g_print ("\n%s\n\n%s\n",
_("Batch mode disabled."),
_("Available interpreters are:"));
for (iter = batch_procedures; iter; iter = iter->next)
{
GimpPlugInProcedure *proc = iter->data;
gchar *locale_name;
locale_name = g_locale_from_utf8 (proc->batch_interpreter_name,
-1, NULL, NULL, NULL);
g_print ("- %s (%s)\n",
gimp_object_get_name (iter->data),
locale_name ? locale_name : proc->batch_interpreter_name);
g_free (locale_name);
}
g_print ("\n%s\n",
_("Specify one of these interpreters as --batch-interpreter option."));
return retval;
}
exit_id = g_signal_connect_after (gimp, "exit",
G_CALLBACK (gimp_batch_exit_after_callback),
NULL);
eval_proc = gimp_pdb_lookup_procedure (gimp->pdb, batch_interpreter);
if (eval_proc)
{
gint i;
retval = EXIT_SUCCESS;
for (i = 0; batch_commands[i]; i++)
{
retval = gimp_batch_run_cmd (gimp, batch_interpreter, eval_proc,
GIMP_RUN_NONINTERACTIVE, batch_commands[i]);
/* In case of several commands, stop and return last
* failed command.
*/
if (retval != EXIT_SUCCESS)
{
g_printerr ("Stopping at failing batch command [%d]: %s\n",
i, batch_commands[i]);
break;
}
}
}
else
{
retval = 69; /* EX_UNAVAILABLE - service unavailable (sysexits.h) */
g_message (_("The batch interpreter '%s' is not available. "
"Batch mode disabled."), batch_interpreter);
}
g_signal_handler_disconnect (gimp, exit_id);
return retval;
}
/*
* The purpose of this handler is to exit GIMP cleanly when the batch
* procedure calls the gimp-exit procedure. Without this callback, the
* message "batch command experienced an execution error" would appear
* and gimp would hang forever.
*/
static void
gimp_batch_exit_after_callback (Gimp *gimp)
{
if (gimp->be_verbose)
g_print ("EXIT: %s\n", G_STRFUNC);
gegl_exit ();
exit (EXIT_SUCCESS);
}
app, libgimp, libgimpbase: plug-in and PDB protocol refactoring part two - Change the wire protocol's GPProcInstall to transmit the entire information needed for constructing all GParamSpecs we use, don't use GimpPDBArgType in GPProcInstall but an enum private to the wire protocol plus the GParamSpec's GType name. Bump the wire protocol version. - Add gimpgpparamspecs.[ch] in both app/plug-in/ and libgimp/ which take care of converting between GPParamDef and GParamSpec. They share code as far as possible. - Change pluginrc writing and parsing to re-use GPParamDef and the utility functions from gimpgpparamspecs. - Remove gimp_pdb_compat_param_spec() from app/pdb/gimp-pdb-compat.[ch], the entire core uses proper GParamSpecs from the wire protocol now, the whole file will follow down the drain once we use a GValue representation on the wire too. - In gimp_plug_in_handle_proc_install(), change the "run-mode" parameter to a GParamSpecEnum(GIMP_TYPE_RUN_MODE) (if it is not already an enum). and change all places in app/ to treat it as an enum value. - plug-ins: fix cml-explorer to register correctly, a typo in "run-mode" was never noticed until now. - Add gimpgpcompat.[ch] in libgimp to deal with all the transforms between old-style wire communication and using GParamSpec and GValue, it contains some functions that are subject to change or even removal in the next steps. - Change the libgimp GimpProcedure and GimpPlugIn in many ways to be able to actually install procedures the new way. - plug-ins: change goat-exercise to completely use the new GimpPlugIn and GimpProcedure API, look here to see how plug-ins will look in the future, of course subject to change until this is finished. - Next: changing GPParam to transmit all information about a GValue.
2019-07-27 16:37:55 +02:00
static inline gboolean
GIMP_IS_PARAM_SPEC_RUN_MODE (GParamSpec *pspec)
{
return (G_IS_PARAM_SPEC_ENUM (pspec) &&
pspec->value_type == GIMP_TYPE_RUN_MODE);
}
static gint
gimp_batch_run_cmd (Gimp *gimp,
const gchar *proc_name,
GimpProcedure *procedure,
GimpRunMode run_mode,
const gchar *cmd)
{
GimpValueArray *args;
GimpValueArray *return_vals;
GError *error = NULL;
gint i = 0;
gint retval = EXIT_SUCCESS;
args = gimp_procedure_get_arguments (procedure);
if (procedure->num_args > i &&
app, libgimp, libgimpbase: plug-in and PDB protocol refactoring part two - Change the wire protocol's GPProcInstall to transmit the entire information needed for constructing all GParamSpecs we use, don't use GimpPDBArgType in GPProcInstall but an enum private to the wire protocol plus the GParamSpec's GType name. Bump the wire protocol version. - Add gimpgpparamspecs.[ch] in both app/plug-in/ and libgimp/ which take care of converting between GPParamDef and GParamSpec. They share code as far as possible. - Change pluginrc writing and parsing to re-use GPParamDef and the utility functions from gimpgpparamspecs. - Remove gimp_pdb_compat_param_spec() from app/pdb/gimp-pdb-compat.[ch], the entire core uses proper GParamSpecs from the wire protocol now, the whole file will follow down the drain once we use a GValue representation on the wire too. - In gimp_plug_in_handle_proc_install(), change the "run-mode" parameter to a GParamSpecEnum(GIMP_TYPE_RUN_MODE) (if it is not already an enum). and change all places in app/ to treat it as an enum value. - plug-ins: fix cml-explorer to register correctly, a typo in "run-mode" was never noticed until now. - Add gimpgpcompat.[ch] in libgimp to deal with all the transforms between old-style wire communication and using GParamSpec and GValue, it contains some functions that are subject to change or even removal in the next steps. - Change the libgimp GimpProcedure and GimpPlugIn in many ways to be able to actually install procedures the new way. - plug-ins: change goat-exercise to completely use the new GimpPlugIn and GimpProcedure API, look here to see how plug-ins will look in the future, of course subject to change until this is finished. - Next: changing GPParam to transmit all information about a GValue.
2019-07-27 16:37:55 +02:00
GIMP_IS_PARAM_SPEC_RUN_MODE (procedure->args[i]))
{
g_value_set_enum (gimp_value_array_index (args, i++), run_mode);
}
if (procedure->num_args > i &&
G_IS_PARAM_SPEC_STRING (procedure->args[i]))
app, libgimp, libgimpbase: plug-in and PDB protocol refactoring part two - Change the wire protocol's GPProcInstall to transmit the entire information needed for constructing all GParamSpecs we use, don't use GimpPDBArgType in GPProcInstall but an enum private to the wire protocol plus the GParamSpec's GType name. Bump the wire protocol version. - Add gimpgpparamspecs.[ch] in both app/plug-in/ and libgimp/ which take care of converting between GPParamDef and GParamSpec. They share code as far as possible. - Change pluginrc writing and parsing to re-use GPParamDef and the utility functions from gimpgpparamspecs. - Remove gimp_pdb_compat_param_spec() from app/pdb/gimp-pdb-compat.[ch], the entire core uses proper GParamSpecs from the wire protocol now, the whole file will follow down the drain once we use a GValue representation on the wire too. - In gimp_plug_in_handle_proc_install(), change the "run-mode" parameter to a GParamSpecEnum(GIMP_TYPE_RUN_MODE) (if it is not already an enum). and change all places in app/ to treat it as an enum value. - plug-ins: fix cml-explorer to register correctly, a typo in "run-mode" was never noticed until now. - Add gimpgpcompat.[ch] in libgimp to deal with all the transforms between old-style wire communication and using GParamSpec and GValue, it contains some functions that are subject to change or even removal in the next steps. - Change the libgimp GimpProcedure and GimpPlugIn in many ways to be able to actually install procedures the new way. - plug-ins: change goat-exercise to completely use the new GimpPlugIn and GimpProcedure API, look here to see how plug-ins will look in the future, of course subject to change until this is finished. - Next: changing GPParam to transmit all information about a GValue.
2019-07-27 16:37:55 +02:00
{
g_value_set_static_string (gimp_value_array_index (args, i++), cmd);
}
return_vals =
gimp_pdb_execute_procedure_by_name_args (gimp->pdb,
gimp_get_user_context (gimp),
NULL, &error,
proc_name, args);
switch (g_value_get_enum (gimp_value_array_index (return_vals, 0)))
{
case GIMP_PDB_EXECUTION_ERROR:
/* Using Linux's standard exit code as found in /usr/include/sysexits.h
* Since other platforms may not have the header, I simply
* hardcode the few cases.
*/
retval = 70; /* EX_SOFTWARE - internal software error */
if (error)
{
g_printerr ("batch command experienced an execution error:\n"
"%s\n", error->message);
}
else
{
g_printerr ("batch command experienced an execution error\n");
}
break;
case GIMP_PDB_CALLING_ERROR:
retval = 64; /* EX_USAGE - command line usage error */
if (error)
{
g_printerr ("batch command experienced a calling error:\n"
"%s\n", error->message);
}
else
{
g_printerr ("batch command experienced a calling error\n");
}
break;
case GIMP_PDB_SUCCESS:
retval = EXIT_SUCCESS;
g_printerr ("batch command executed successfully\n");
break;
case GIMP_PDB_CANCEL:
/* Not in sysexits.h, but usually used for 'Script terminated by
* Control-C'. See: https://tldp.org/LDP/abs/html/exitcodes.html
*/
retval = 130;
break;
case GIMP_PDB_PASS_THROUGH:
retval = EXIT_FAILURE; /* Catchall. */
break;
}
gimp_value_array_unref (return_vals);
gimp_value_array_unref (args);
if (error)
g_error_free (error);
return retval;
}