gimp/app/batch.c

205 lines
5.7 KiB
C
Raw 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 <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <stdlib.h>
#include <gegl.h>
#include "core/core-types.h"
#include "base/tile-swap.h"
#include "core/gimp.h"
#include "core/gimpparamspecs.h"
#include "batch.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 17:13:47 +08:00
#include "pdb/gimppdb.h"
#include "pdb/gimpprocedure.h"
#include "gimp-intl.h"
#define BATCH_DEFAULT_EVAL_PROC "plug-in-script-fu-eval"
static void batch_exit_after_callback (Gimp *gimp) G_GNUC_NORETURN;
static void batch_run_cmd (Gimp *gimp,
const gchar *proc_name,
GimpProcedure *procedure,
GimpRunMode run_mode,
const gchar *cmd);
void
batch_run (Gimp *gimp,
const gchar *batch_interpreter,
const gchar **batch_commands)
{
gulong exit_id;
if (! batch_commands || ! batch_commands[0])
return;
exit_id = g_signal_connect_after (gimp, "exit",
G_CALLBACK (batch_exit_after_callback),
NULL);
if (! batch_interpreter)
{
batch_interpreter = g_getenv ("GIMP_BATCH_INTERPRETER");
if (! batch_interpreter)
{
batch_interpreter = BATCH_DEFAULT_EVAL_PROC;
if (gimp->be_verbose)
g_printerr (_("No batch interpreter specified, using the default "
"'%s'.\n"), batch_interpreter);
}
}
/* script-fu text console, hardcoded for backward compatibility */
if (strcmp (batch_interpreter, "plug-in-script-fu-eval") == 0 &&
strcmp (batch_commands[0], "-") == 0)
{
const gchar *proc_name = "plug-in-script-fu-text-console";
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 17:13:47 +08:00
GimpProcedure *procedure = gimp_pdb_lookup_procedure (gimp->pdb,
proc_name);
if (procedure)
batch_run_cmd (gimp, proc_name, procedure,
GIMP_RUN_NONINTERACTIVE, NULL);
else
g_message (_("The batch interpreter '%s' is not available. "
"Batch mode disabled."), proc_name);
}
else
{
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 17:13:47 +08:00
GimpProcedure *eval_proc = gimp_pdb_lookup_procedure (gimp->pdb,
batch_interpreter);
if (eval_proc)
{
gint i;
for (i = 0; batch_commands[i]; i++)
batch_run_cmd (gimp, batch_interpreter, eval_proc,
GIMP_RUN_NONINTERACTIVE, batch_commands[i]);
}
else
{
g_message (_("The batch interpreter '%s' is not available. "
"Batch mode disabled."), batch_interpreter);
}
}
g_signal_handler_disconnect (gimp, exit_id);
}
/*
* 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
batch_exit_after_callback (Gimp *gimp)
{
if (gimp->be_verbose)
g_print ("EXIT: %s\n", G_STRFUNC);
gegl_exit ();
/* make sure that the swap files are removed before we quit */
tile_swap_exit ();
exit (EXIT_SUCCESS);
}
static void
batch_run_cmd (Gimp *gimp,
const gchar *proc_name,
GimpProcedure *procedure,
GimpRunMode run_mode,
const gchar *cmd)
{
GValueArray *args;
GValueArray *return_vals;
GError *error = NULL;
gint i = 0;
args = gimp_procedure_get_arguments (procedure);
if (procedure->num_args > i && GIMP_IS_PARAM_SPEC_INT32 (procedure->args[i]))
g_value_set_int (&args->values[i++], run_mode);
if (procedure->num_args > i && GIMP_IS_PARAM_SPEC_STRING (procedure->args[i]))
g_value_set_static_string (&args->values[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 (&return_vals->values[0]))
{
case GIMP_PDB_EXECUTION_ERROR:
if (error)
{
g_printerr ("batch command experienced an execution error: %s\n",
error->message);
}
else
{
g_printerr ("batch command experienced an execution error\n");
}
break;
case GIMP_PDB_CALLING_ERROR:
if (error)
{
g_printerr ("batch command experienced a calling error: %s\n",
error->message);
}
else
{
g_printerr ("batch command experienced a calling error\n");
}
break;
case GIMP_PDB_SUCCESS:
g_printerr ("batch command executed successfully\n");
break;
}
g_value_array_free (return_vals);
g_value_array_free (args);
if (error)
g_error_free (error);
return;
}