Commit 103a7e4f authored by Michael Natterer's avatar Michael Natterer 😴

plug-ins: port script-fu to GimpPlugIn

This probably has some bugs, but worked flawlessly in my tests...
parent 85d9a291
This diff is collapsed.
......@@ -62,7 +62,6 @@ enum
/*
* Local Functions
*/
static void script_fu_console_interface (void);
static void script_fu_console_response (GtkWidget *widget,
gint response_id,
ConsoleInterface *console);
......@@ -92,27 +91,9 @@ static void script_fu_output_to_console (TsOutputType type,
* Function definitions
*/
void
script_fu_console_run (const gchar *name,
gint nparams,
const GimpParam *params,
gint *nreturn_vals,
GimpParam **return_vals)
{
static GimpParam values[1];
ts_set_print_flag (1);
script_fu_console_interface ();
*nreturn_vals = 1;
*return_vals = values;
values[0].type = GIMP_PDB_STATUS;
values[0].data.d_status = GIMP_PDB_SUCCESS;
}
static void
script_fu_console_interface (void)
GimpValueArray *
script_fu_console_run (GimpProcedure *procedure,
const GimpValueArray *args)
{
ConsoleInterface console = { 0, };
GtkWidget *vbox;
......@@ -120,6 +101,8 @@ script_fu_console_interface (void)
GtkWidget *scrolled_window;
GtkWidget *hbox;
ts_set_print_flag (1);
gimp_ui_init ("script-fu", FALSE);
console.history_max = 50;
......@@ -254,6 +237,8 @@ script_fu_console_interface (void)
if (console.dialog)
gtk_widget_destroy (console.dialog);
return gimp_procedure_new_return_values (procedure, GIMP_PDB_SUCCESS, NULL);
}
static void
......@@ -401,20 +386,13 @@ script_fu_browse_response (GtkWidget *widget,
gint response_id,
ConsoleInterface *console)
{
GimpProcBrowserDialog *dialog = GIMP_PROC_BROWSER_DIALOG (widget);
gchar *proc_name;
gchar *proc_blurb;
gchar *proc_help;
gchar *proc_author;
gchar *proc_copyright;
gchar *proc_date;
GimpPDBProcType proc_type;
gint n_params;
gint n_return_vals;
GimpParamDef *params;
GimpParamDef *return_vals;
gint i;
GString *text;
GimpProcBrowserDialog *dialog = GIMP_PROC_BROWSER_DIALOG (widget);
GimpProcedure *procedure;
gchar *proc_name;
GParamSpec **pspecs;
gint n_pspecs;
gint i;
GString *text;
if (response_id != GTK_RESPONSE_APPLY)
{
......@@ -427,25 +405,17 @@ script_fu_browse_response (GtkWidget *widget,
if (proc_name == NULL)
return;
gimp_pdb_proc_info (proc_name,
&proc_blurb,
&proc_help,
&proc_author,
&proc_copyright,
&proc_date,
&proc_type,
&n_params,
&n_return_vals,
&params,
&return_vals);
procedure = gimp_pdb_lookup_procedure (gimp_get_pdb (), proc_name);
pspecs = gimp_procedure_get_arguments (procedure, &n_pspecs);
text = g_string_new ("(");
text = g_string_append (text, proc_name);
for (i = 0; i < n_params; i++)
for (i = 0; i < n_pspecs; i++)
{
text = g_string_append_c (text, ' ');
text = g_string_append (text, params[i].name);
text = g_string_append (text, pspecs[i]->name);
}
text = g_string_append_c (text, ')');
......@@ -463,14 +433,6 @@ script_fu_browse_response (GtkWidget *widget,
gtk_window_present (GTK_WINDOW (console->dialog));
g_free (proc_name);
g_free (proc_blurb);
g_free (proc_help);
g_free (proc_author);
g_free (proc_copyright);
g_free (proc_date);
gimp_destroy_paramdefs (params, n_params);
gimp_destroy_paramdefs (return_vals, n_return_vals);
}
static void
......@@ -606,7 +568,8 @@ script_fu_cc_key_function (GtkWidget *widget,
output = g_string_new (NULL);
ts_register_output_func (ts_gstring_output_func, output);
gimp_plugin_set_pdb_error_handler (GIMP_PDB_ERROR_HANDLER_PLUGIN);
gimp_plug_in_set_pdb_error_handler (gimp_get_plug_in (),
GIMP_PDB_ERROR_HANDLER_PLUGIN);
if (ts_interpret_string (list->data) != 0)
{
......@@ -623,7 +586,8 @@ script_fu_cc_key_function (GtkWidget *widget,
console);
}
gimp_plugin_set_pdb_error_handler (GIMP_PDB_ERROR_HANDLER_INTERNAL);
gimp_plug_in_set_pdb_error_handler (gimp_get_plug_in (),
GIMP_PDB_ERROR_HANDLER_INTERNAL);
g_string_free (output, TRUE);
......
......@@ -19,11 +19,8 @@
#define __SCRIPT_FU_CONSOLE_H__
void script_fu_console_run (const gchar *name,
gint nparams,
const GimpParam *params,
gint *nreturn_vals,
GimpParam **return_vals);
GimpValueArray * script_fu_console_run (GimpProcedure *procedure,
const GimpValueArray *args);
#endif /* __SCRIPT_FU_CONSOLE_H__ */
......@@ -25,24 +25,17 @@
#include "script-fu-intl.h"
void
script_fu_eval_run (const gchar *name,
gint nparams,
const GimpParam *params,
gint *nreturn_vals,
GimpParam **return_vals)
GimpValueArray *
script_fu_eval_run (GimpProcedure *procedure,
const GimpValueArray *args)
{
static GimpParam values[2];
GString *output = g_string_new (NULL);
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
GimpRunMode run_mode;
const gchar *code;
*nreturn_vals = 1;
*return_vals = values;
values[0].type = GIMP_PDB_STATUS;
run_mode = params[0].data.d_int32;
run_mode = g_value_get_enum (gimp_value_array_index (args, 0));
code = g_value_get_string (gimp_value_array_index (args, 1));
ts_set_run_mode (run_mode);
ts_register_output_func (ts_gstring_output_func, output);
......@@ -50,7 +43,7 @@ script_fu_eval_run (const gchar *name,
switch (run_mode)
{
case GIMP_RUN_NONINTERACTIVE:
if (ts_interpret_string (params[1].data.d_string) != 0)
if (ts_interpret_string (code) != 0)
status = GIMP_PDB_EXECUTION_ERROR;
break;
......@@ -65,16 +58,15 @@ script_fu_eval_run (const gchar *name,
break;
}
values[0].data.d_status = status;
if (status != GIMP_PDB_SUCCESS && output->len > 0)
{
*nreturn_vals = 2;
values[1].type = GIMP_PDB_STRING;
values[1].data.d_string = g_string_free (output, FALSE);
}
else
{
g_string_free (output, TRUE);
GError *error = g_error_new_literal (0, 0,
g_string_free (output, FALSE));
return gimp_procedure_new_return_values (procedure, status, error);
}
g_string_free (output, TRUE);
return gimp_procedure_new_return_values (procedure, status, NULL);
}
......@@ -19,11 +19,8 @@
#define __SCRIPT_FU_EVAL_H__
void script_fu_eval_run (const gchar *name,
gint nparams,
const GimpParam *params,
gint *nreturn_vals,
GimpParam **return_vals);
GimpValueArray * script_fu_eval_run (GimpProcedure *procedure,
const GimpValueArray *args);
#endif /* __SCRIPT_FU_EVAL_H__ */
......@@ -880,7 +880,8 @@ script_fu_ok (SFScript *script)
output = g_string_new (NULL);
ts_register_output_func (ts_gstring_output_func, output);
gimp_plugin_set_pdb_error_handler (GIMP_PDB_ERROR_HANDLER_PLUGIN);
gimp_plug_in_set_pdb_error_handler (gimp_get_plug_in (),
GIMP_PDB_ERROR_HANDLER_PLUGIN);
if (ts_interpret_string (command))
{
......@@ -891,7 +892,8 @@ script_fu_ok (SFScript *script)
g_free (message);
}
gimp_plugin_set_pdb_error_handler (GIMP_PDB_ERROR_HANDLER_INTERNAL);
gimp_plug_in_set_pdb_error_handler (gimp_get_plug_in (),
GIMP_PDB_ERROR_HANDLER_INTERNAL);
g_string_free (output, TRUE);
......
This diff is collapsed.
......@@ -19,31 +19,32 @@
#define __SCRIPT_FU_SCRIPT_H__
SFScript * script_fu_script_new (const gchar *name,
const gchar *menu_label,
const gchar *blurb,
const gchar *author,
const gchar *copyright,
const gchar *date,
const gchar *image_types,
gint n_args);
void script_fu_script_free (SFScript *script);
void script_fu_script_install_proc (SFScript *script,
GimpRunProc run_proc);
void script_fu_script_uninstall_proc (SFScript *script);
gchar * script_fu_script_get_title (SFScript *script);
void script_fu_script_reset (SFScript *script,
gboolean reset_ids);
gint script_fu_script_collect_standard_args (SFScript *script,
gint n_params,
const GimpParam *params);
gchar * script_fu_script_get_command (SFScript *script);
gchar * script_fu_script_get_command_from_params (SFScript *script,
const GimpParam *params);
SFScript * script_fu_script_new (const gchar *name,
const gchar *menu_label,
const gchar *blurb,
const gchar *authors,
const gchar *copyright,
const gchar *date,
const gchar *image_types,
gint n_args);
void script_fu_script_free (SFScript *script);
void script_fu_script_install_proc (GimpPlugIn *plug_in,
SFScript *script,
GimpRunFunc run_func);
void script_fu_script_uninstall_proc (GimpPlugIn *plug_in,
SFScript *script);
gchar * script_fu_script_get_title (SFScript *script);
void script_fu_script_reset (SFScript *script,
gboolean reset_ids);
gint script_fu_script_collect_standard_args (SFScript *script,
const GimpValueArray *args);
gchar * script_fu_script_get_command (SFScript *script);
gchar * script_fu_script_get_command_from_params (SFScript *script,
const GimpValueArray *args);
#endif /* __SCRIPT_FU_SCRIPT__ */
......@@ -54,28 +54,26 @@ typedef struct
* Local Functions
*/
static gboolean script_fu_run_command (const gchar *command,
GError **error);
static void script_fu_load_directory (GFile *directory);
static void script_fu_load_script (GFile *file);
static gboolean script_fu_install_script (gpointer foo,
GList *scripts,
gpointer bar);
static void script_fu_install_menu (SFMenu *menu);
static gboolean script_fu_remove_script (gpointer foo,
GList *scripts,
gpointer bar);
static void script_fu_script_proc (const gchar *name,
gint nparams,
const GimpParam *params,
gint *nreturn_vals,
GimpParam **return_vals);
static SFScript *script_fu_find_script (const gchar *name);
static gchar * script_fu_menu_map (const gchar *menu_path);
static gint script_fu_menu_compare (gconstpointer a,
gconstpointer b);
static gboolean script_fu_run_command (const gchar *command,
GError **error);
static void script_fu_load_directory (GFile *directory);
static void script_fu_load_script (GFile *file);
static gboolean script_fu_install_script (gpointer foo,
GList *scripts,
gpointer data);
static void script_fu_install_menu (SFMenu *menu);
static gboolean script_fu_remove_script (gpointer foo,
GList *scripts,
gpointer data);
static GimpValueArray * script_fu_script_proc (GimpProcedure *procedure,
const GimpValueArray *args,
gpointer data);
static SFScript * script_fu_find_script (const gchar *name);
static gchar * script_fu_menu_map (const gchar *menu_path);
static gint script_fu_menu_compare (gconstpointer a,
gconstpointer b);
/*
......@@ -91,7 +89,8 @@ static GList *script_menu_list = NULL;
*/
void
script_fu_find_scripts (GList *path)
script_fu_find_scripts (GimpPlugIn *plug_in,
GList *path)
{
GList *list;
......@@ -100,7 +99,7 @@ script_fu_find_scripts (GList *path)
{
g_tree_foreach (script_tree,
(GTraverseFunc) script_fu_remove_script,
NULL);
plug_in);
g_tree_destroy (script_tree);
}
......@@ -117,7 +116,7 @@ script_fu_find_scripts (GList *path)
/* Now that all scripts are read in and sorted, tell gimp about them */
g_tree_foreach (script_tree,
(GTraverseFunc) script_fu_install_script,
NULL);
plug_in);
script_menu_list = g_list_sort (script_menu_list,
(GCompareFunc) script_fu_menu_compare);
......@@ -672,15 +671,17 @@ script_fu_load_script (GFile *file)
static gboolean
script_fu_install_script (gpointer foo G_GNUC_UNUSED,
GList *scripts,
gpointer bar G_GNUC_UNUSED)
gpointer data)
{
GList *list;
GimpPlugIn *plug_in = data;
GList *list;
for (list = scripts; list; list = g_list_next (list))
{
SFScript *script = list->data;
script_fu_script_install_proc (script, script_fu_script_proc);
script_fu_script_install_proc (plug_in, script,
script_fu_script_proc);
}
return FALSE;
......@@ -689,7 +690,13 @@ script_fu_install_script (gpointer foo G_GNUC_UNUSED,
static void
script_fu_install_menu (SFMenu *menu)
{
gimp_plugin_menu_register (menu->script->name, menu->menu_path);
GimpPlugIn *plug_in = gimp_get_plug_in ();
GimpProcedure *procedure;
procedure = gimp_plug_in_get_temp_procedure (plug_in,
menu->script->name);
gimp_procedure_add_menu_path (procedure, menu->menu_path);
g_free (menu->menu_path);
g_slice_free (SFMenu, menu);
......@@ -701,15 +708,16 @@ script_fu_install_menu (SFMenu *menu)
static gboolean
script_fu_remove_script (gpointer foo G_GNUC_UNUSED,
GList *scripts,
gpointer bar G_GNUC_UNUSED)
gpointer data)
{
GList *list;
GimpPlugIn *plug_in = data;
GList *list;
for (list = scripts; list; list = g_list_next (list))
{
SFScript *script = list->data;
script_fu_script_uninstall_proc (script);
script_fu_script_uninstall_proc (plug_in, script);
script_fu_script_free (script);
}
......@@ -718,120 +726,96 @@ script_fu_remove_script (gpointer foo G_GNUC_UNUSED,
return FALSE;
}
static void
script_fu_script_proc (const gchar *name,
gint nparams,
const GimpParam *params,
gint *nreturn_vals,
GimpParam **return_vals)
static GimpValueArray *
script_fu_script_proc (GimpProcedure *procedure,
const GimpValueArray *args,
gpointer data)
{
static GimpParam values[2] = { { 0, }, { 0, } };
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
SFScript *script;
GError *error = NULL;
if (values[1].type == GIMP_PDB_STRING && values[1].data.d_string)
{
g_free (values[1].data.d_string);
values[1].data.d_string = NULL;
}
GimpRunMode run_mode;
GError *error = NULL;
*nreturn_vals = 1;
*return_vals = values;
script = script_fu_find_script (gimp_procedure_get_name (procedure));
values[0].type = GIMP_PDB_STATUS;
if (! script)
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_CALLING_ERROR,
NULL);
script = script_fu_find_script (name);
run_mode = g_value_get_enum (gimp_value_array_index (args, 0));
if (! script)
status = GIMP_PDB_CALLING_ERROR;
ts_set_run_mode (run_mode);
if (status == GIMP_PDB_SUCCESS)
switch (run_mode)
{
GimpRunMode run_mode = params[0].data.d_int32;
case GIMP_RUN_INTERACTIVE:
{
gint min_args = 0;
ts_set_run_mode (run_mode);
/* First, try to collect the standard script arguments... */
min_args = script_fu_script_collect_standard_args (script, args);
switch (run_mode)
{
case GIMP_RUN_INTERACTIVE:
/* ...then acquire the rest of arguments (if any) with a dialog */
if (script->n_args > min_args)
{
gint min_args = 0;
/* First, try to collect the standard script arguments... */
min_args = script_fu_script_collect_standard_args (script,
nparams, params);
/* ...then acquire the rest of arguments (if any) with a dialog */
if (script->n_args > min_args)
{
status = script_fu_interface (script, min_args);
break;
}
/* otherwise (if the script takes no more arguments), skip
* this part and run the script directly (fallthrough)
*/
status = script_fu_interface (script, min_args);
break;
}
/* otherwise (if the script takes no more arguments), skip
* this part and run the script directly (fallthrough)
*/
}
case GIMP_RUN_NONINTERACTIVE:
/* Make sure all the arguments are there */
if (nparams != (script->n_args + 1))
status = GIMP_PDB_CALLING_ERROR;
case GIMP_RUN_NONINTERACTIVE:
/* Make sure all the arguments are there */
if (gimp_value_array_length (args) != (script->n_args + 1))
status = GIMP_PDB_CALLING_ERROR;
if (status == GIMP_PDB_SUCCESS)
{
gchar *command;
command = script_fu_script_get_command_from_params (script,
params);
/* run the command through the interpreter */
if (! script_fu_run_command (command, &error))
{
status = GIMP_PDB_EXECUTION_ERROR;
*nreturn_vals = 2;
values[1].type = GIMP_PDB_STRING;
values[1].data.d_string = error->message;
if (status == GIMP_PDB_SUCCESS)
{
gchar *command;
error->message = NULL;
g_error_free (error);
}
command = script_fu_script_get_command_from_params (script, args);
g_free (command);
/* run the command through the interpreter */
if (! script_fu_run_command (command, &error))
{
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_EXECUTION_ERROR,
error);
}
break;
case GIMP_RUN_WITH_LAST_VALS:
{
gchar *command;
/* First, try to collect the standard script arguments */
script_fu_script_collect_standard_args (script, nparams, params);
g_free (command);
}
break;
command = script_fu_script_get_command (script);
case GIMP_RUN_WITH_LAST_VALS:
{
gchar *command;
/* run the command through the interpreter */
if (! script_fu_run_command (command, &error))
{
status = GIMP_PDB_EXECUTION_ERROR;
*nreturn_vals = 2;
values[1].type = GIMP_PDB_STRING;
values[1].data.d_string = error->message;
/* First, try to collect the standard script arguments */
script_fu_script_collect_standard_args (script, args);
error->message = NULL;
g_error_free (error);
}
command = script_fu_script_get_command (script);
g_free (command);
/* run the command through the interpreter */
if (! script_fu_run_command (command, &error))
{
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_EXECUTION_ERROR,
error);
}
break;
default:
break;
}
g_free (command);
}
break;
default:
break;
}
values[0].data.d_status = status;
return gimp_procedure_new_return_values (procedure, status, NULL);
}
/* this is a GTraverseFunction */
......
......@@ -19,11 +19,12 @@
#define __SCRIPT_FU_SCRIPTS_H__
void script_fu_find_scripts (GList *path);
pointer script_fu_add_script (scheme *sc,
pointer a);
pointer script_fu_add_menu (scheme *sc,
pointer a);
void script_fu_find_scripts (GimpPlugIn *plug_in,
GList *path);
pointer script_fu_add_script (scheme *sc,
pointer a);
pointer script_fu_add_menu (scheme *sc,
pointer a);
#endif /* __SCRIPT_FU_SCRIPTS__ */
......@@ -219,18 +219,20 @@ script_fu_server_get_mode (void)
return server_mode;
}
void
script_fu_server_run (const gchar *name,
gint nparams,
const GimpParam *params,
gint *nreturn_vals,
GimpParam **return_vals)
GimpValueArray *
script_fu_server_run (GimpProcedure *procedure,
const GimpValueArray *args)
{
static GimpParam values[1];
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
GimpRunMode run_mode;
const gchar *ip;
gint port;
const gchar *logfile;
run_mode = params[0].data.d_int32;
run_mode = g_value_get_enum (gimp_value_array_index (args, 0));
ip = g_value_get_string (gimp_value_array_index (args, 1));
port = g_value_get_int (gimp_value_array_index (args, 2));
logfile = g_value_get_string (gimp_value_array_index (args, 3));
ts_set_run_mode (run_mode);
ts_set_print_flag (1);
......@@ -252,26 +254,19 @@ script_fu_server_run (const gchar *name,
server_mode = TRUE;
/* Start the server */
server_start ((params[1].data.d_string &&
strlen (params[1].data.d_string)) ?
params[1].data.d_string : "127.0.0.1",
params[2].data.d_int32,
params[3].data.d_string);
server_start (ip ? ip : "127.0.0.1", port, logfile);
break;
case GIMP_RUN_WITH_LAST_VALS:
status = GIMP_PDB_CALLING_ERROR;
g_warning ("Script-Fu server does not handle \"GIMP_RUN_WITH_LAST_VALS\"");
g_printerr ("Script-Fu server does not handle "
"\"GIMP_RUN_WITH_LAST_VALS\"\n");
default:
break;
}
*nreturn_vals = 1;
*return_vals = values;
values[0].type = GIMP_PDB_STATUS;
values[0].data.d_status = status;
return gimp_procedure_new_return_values (procedure, status, NULL);
}
static void
......
......@@ -19,14 +19,11 @@
#define __SCRIPT_FU_SERVER_H__
void script_fu_server_run (const gchar *name,
gint nparams,
const GimpParam *params,
gint *nreturn_vals,
GimpParam **return_vals);